我正在尝试生成一个字符串,说明截止日期剩余时间,但由于日历的不一致,我无法弄清楚如何计算出时间。
我想输出一个字符串,例如“1个月,6天和10个小时”。
这就是我所拥有的:
this.GetRemainingTimeString = function()
{
if(this.IsPastDeadline())
return 'Past Deadline';
var str = '';
var diffMS = - this.EndDate - (new Date()).getTime();
var diff = new Date(diffMS);
var defDate = new Date(0);
var years = diff.getFullYear() - defDate.getFullYear();
var months = diff.getMonth();
var days = diff.getDate();
if(years > 0)
{
str += years + ' Year' + (years != 1 ? 's' : '') + ' ';
}
if(months != 0)
{
str += months + ' Month' + (months != 1 ? 's' : '');
}
if(days != 1)
{
str += days + ' Day' + (days != 1 ? 's' : '');
}
return str;
};