我以7周的时间范围开始并结束。我试图在范围大于7天时检查条件,我想对它应用不同的timeFormat:Ex: JS:
startdate = "2015-07-04T00:00:00";
enddate = "2015-07-11T23:59:59"; //range 7 days
//pseudo code:
if (difference between (startdate & enddate) > 7){
//set timeFormat = "30days"
}else{
//set timeFormat = "7days"
}
关于如何实现这一点的任何想法?谢谢!
答案 0 :(得分:2)
var startdate = new Date("2015-07-04T00:00:00");
var enddate = new Date("2015-07-11T23:59:59");
var timeDifference = Math.abs(enddate.getTime() - startdate.getTime());
var differenceDays = Math.ceil(timeDifference / (1000 * 3600 * 24)); //return 8
if (differenceDays > 7){
//set timeFormat = "30days"
}else{
//set timeFormat = "7days"
}
这是Fiddle。
答案 1 :(得分:1)
我会做类似
的事情
var startdate = new Date( "2015-07-04T00:00:00");
var enddate = new Date( "2015-07-11T23:59:59");
var timeDiff = Math.abs(enddate.getTime() - startdate.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
alert(diffDays);
//pseudo code:
if (diffDays > 7){
//set timeFormat = "30days"
alert(3);
}else{
//set timeFormat = "7days"
alert(7)
}