如何使用jquery / js检查时间范围是否大于7周的时间范围

时间:2015-07-11 23:30:34

标签: javascript jquery

我以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"
 }

关于如何实现这一点的任何想法?谢谢!

2 个答案:

答案 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)
 }