我的开始日期和结束日期有7周的时间范围。我正在尝试检查范围大于7天的情况,我想对其应用不同的timeFormat:例如:
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"
 }


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

最佳答案

嗨兄弟看看这个解决方案,希望对您有帮助:

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

08-28 17:39