我有这个功能
function timeCalculator(form)
{
var currentTimeAndDate = new Date();
var userTimeAndDate = form.userDate.value;
var userUTCOffset2 = form.userUTCOffset.value;
var userDate = new Date(userTimeAndDate);
var tzDifference = currentTimeAndDate.getTimezoneOffset() + (userUTCOffset2 - currentTimeAndDate.getTimezoneOffset());
var timeDifference = new Date((userDate.getTime()+(tzDifference*60*1000))-(currentTimeAndDate.getTime()+(userDate.getTimezoneOffset()*60*1000)));
var hours = Math.floor(timeDifference.getTime() / 36e5),
minutes = Math.floor(timeDifference.getTime() % 36e5 / 60000),
seconds = Math.floor(timeDifference.getTime() % 60000 / 1000);
document.getElementById('result').innerHTML = "There is exactly "+hours+" hours,"+minutes+"minutes and "+seconds+" seconds left until your specified time.";
}
这应该做的是用户在表单中输入时间、日期和 UTC 偏移量,并据此计算距离该点还剩多少时间。分钟和秒似乎工作正常,但小时有问题。任何人有任何建议或者可能对这样的事情有不同的方法吗?另外,我怎样才能在差异中添加天、月、年?