0

这就是我到目前为止使用 jquery 所拥有的。我不知道为什么我的 checkRecord 按钮不会显示两个日期之间的天数。我显然错过了一些东西。

$(document).ready(function () {
'use strict';      
var monthNames = [ "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" ];
var dayNames= ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]

var newDate = new Date();
 newDate.setDate(newDate.getDate()); 

$('#safetyRecord').hide();

$('#today').html(dayNames[newDate.getDay()] + "," +' ' + monthNames[newDate.getMonth()] + ' ' + newDate.getDate() + ","+ ' ' + newDate.getFullYear());

$('#checkRecord').click(function(){
var $daysSinceLastAccident = $('#daysSinceLastAccident');

var dateOfLastAccident = new Date($('#dateOfLastAccident').val());

var today = new Date();

$daysSinceLastAccident = Math.floor((today.getTime() - dateOfLastAccident.getTime()) / (24 * 60 * 60 * 1000));

$daysSinceLastAccident.text(daysSinceLastAccident);
$('#safetyRecord').show();
});

});
4

3 回答 3

1

尝试改变:

var today = new date;$('#safetyRecord').show;

var today = new Date();$('#safetyRecord').show();

于 2013-10-08T22:24:31.490 回答
0

在您的代码中:

>  newDate.setDate(newDate.getDate()); 

什么都不做,它只是将newDate的日期设置为其当前值。

> var $daysSinceLastAccident = $('#daysSinceLastAccident');
> [...] 
> $daysSinceLastAccident = Math.floor((today.getTime() - ...

最初为 $daysSinceLastAccident分配了一个对 jQuery 对象的引用,然后是一个数字。最初分配的目的是什么?

> $daysSinceLastAccident.text(daysSinceLastAccident);

但您似乎希望它仍然是一个 jQuery 对象。

还:

> var today = new Date();

已经有一个newDate变量可能具有完全相同的值,可能早 1 毫秒,已用于显示当前日期。似乎没有任何理由不再使用它。

请注意,如果您想要以天为单位的两个日期对象之间的差异,您可以将时间设置为相关日期的中午,然后减去并除以 ms/day,然后四舍五入。

例如以上:

var newDate = new Date();
newDate.setHours(12,0,0,0); // 12:00:00.000

// You should never leave parsing of date strings to the Date object
// I'll assume here that it "works" in the limited cases you've tested
var dateOfLastAccident = new Date($('#dateOfLastAccident').val());
dateOfLastAccident.setHours(12,0,0,0);

var daysSinceLastAccident = Math.round((newDate - dateOfLastAccident) / 8.64e7);

您应该手动解析日期字符串,这并不难。例如,给定输入,例如 2013-10-09,您可以执行以下操作:

function parseISODateString(s) {
  s = s.split(/\D/g);
  var d = new Date(s[0], --s[1], s[2]);

  // Return d if s was a valid date string, NaN otherwise.
  return (d && d.getFullYear() == s[0] && d.getDate() == s[2])? d : NaN;
}

您应该测试输入字符串和返回值以使其健壮。

于 2013-10-08T22:56:50.457 回答
0

这是以年、月和日为单位获取不同日期持续时间的最简单功能...检查此代码

function getDatesDiff(from,to){
    from = new Date(from);
    to = new Date(to);

     if (isNaN(to - from)) return "";

    from.setDate(from.getDate()-1);
    var fullMonths=0;
    var days=0;
    var fromDate=new Date(from);
    var finalDate=new Date(fromDate);
    var nextMonth=new Date(new Date(fromDate).setMonth(new Date(fromDate).getMonth() + 1));
    var result='';

    while(from<=to){
         if(+from == +nextMonth){
               fullMonths++;
               finalDate=new Date(nextMonth);
               nextMonth.setMonth(nextMonth.getMonth() + 1);
        }
              from.setDate(from.getDate() + 1);

    }


     days=Math.round((to-finalDate)/(1000*60*60*24));

      if(Math.round(fullMonths/12)>0)
        result=Math.round(fullMonths/12) + "year"+(Math.round(fullMonths/12)>1?'s':'');

      if(parseInt(fullMonths%12)>0)
        result+=" "+fullMonths%12 + "month"+(parseInt(fullMonths%12)>1?'s':'')  ;

        if(days>0)
            result+=" "+ days + "day"+(days>1?'s':'');

    return result;
}

这是jsfiddle.......

https://jsfiddle.net/1rxnmvnd/
于 2016-05-12T04:52:19.680 回答