这是我到目前为止使用的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();
});
});
答案 0 :(得分:1)
尝试更改:
var today = new date;
和$('#safetyRecord').show;
到
var today = new Date();
和$('#safetyRecord').show();
答案 1 :(得分: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 变量可能具有完全相同的值,可能早于1ms,用于显示当前日期。似乎没有任何理由不再使用它。
请注意,如果您想要以天为单位的两个日期对象之间的差异,您可以在相关日期将时间设置为中午,减去并除以ms /天,然后再舍入。
如上所述:
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;
}
您应该测试输入字符串并返回值以使其健壮。
答案 2 :(得分: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/