使用 jquery 获取两个日期之间的差异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5847165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get difference between two dates using jquery
提问by Simpanoz
Can any one suggest a jQuery plugin for calculating the difference between two dates (dates may contain time also) and show it as '32 days', '13 hours', '20 min' etc?
任何人都可以建议一个 jQuery 插件来计算两个日期之间的差异(日期也可能包含时间)并将其显示为“32 天”、“13 小时”、“20 分钟”等?
回答by Dan
You can add, subtract and do many other things with the nativeJavaScript Dateobject. It's powerful enoughfor most of your needs. If using it, you save kilobytes of page size and make the code understandable for everyone (probably 90% of JavaScript developers have never used any plugins to calculate dates)
您可以使用原生JavaScript Date对象添加、减去和执行许多其他操作。它足够强大,可以满足您的大部分需求。如果使用它,您可以节省 KB 的页面大小并使代码易于理解(可能 90% 的 JavaScript 开发人员从未使用任何插件来计算日期)
The thing I hate about Date object is it does not have a built-in formatted output.
我讨厌 Date 对象的一点是它没有内置的格式化输出。
For example, you cannot tell the localized day of the week or month name without string parsing. Then datejscomes to help you.
例如,如果不进行字符串解析,您就无法分辨本地化的星期几或月份名称。那么datejs来帮助你。
What can a Date object do?
Date 对象可以做什么?
var msMinute = 60*1000,
msDay = 60*60*24*1000,
a = new Date(2012, 2, 12, /* days, hours*/ 23, 59, 59),
b = new Date("2013 march 12"), /* string */
c = new Date(), /* now */
d = new Date(c.getTime() + msDay - msMinute); /* tomorrow - minute */
console.log(a.getUTCHours());
console.log(typeof (b - a + 1000));
console.log(Math.floor((b - a) / msDay) + ' full days between');
console.log(Math.floor(((b - a) % msDay) / msMinute) + ' full minutes between');
console.log('Today is ' + c.getDay() + ' day of week');
console.log('Tomorrow is ' + d.getDay() + ' day of week');
console.log('Your timezone offset is ' + c.getTimezoneOffset() + ' minutes');
Easily calculate days till Christmas
And, sometimes there is more truth in a joke then you could ever expect
而且,有时笑话中的真相比你想象的要多
回答by mVChr
Here's a pretty simple Javascript implementation I just hacked up. You can do the math to extend this to months or years or remove the plurals for 1 values if needed.
这是我刚刚编写的一个非常简单的 Javascript 实现。如果需要,您可以进行数学计算以将其扩展到数月或数年,或者删除 1 值的复数形式。
var dateDiff = function ( d1, d2 ) {
var diff = Math.abs(d1 - d2);
if (Math.floor(diff/86400000)) {
return Math.floor(diff/86400000) + " days";
} else if (Math.floor(diff/3600000)) {
return Math.floor(diff/3600000) + " hours";
} else if (Math.floor(diff/60000)) {
return Math.floor(diff/60000) + " minutes";
} else {
return "< 1 minute";
}
};
dateDiff(new Date(1990, 1, 1), new Date(1990, 1, 13)) // -> 12 days
回答by Hussein
I highly recommend using the excellent datejs frameworkto easily do all your date time calculations
我强烈建议使用优秀的datejs 框架来轻松完成所有日期时间计算
回答by entropo
I think jQuery EasyDateis exactly what you're looking for.
我认为jQuery EasyDate正是您要寻找的。
回答by entropo
day1= $("#tMTLCLsDay").val();
month1= $("#tMTLCLsMnth").val();
year1= $("#tMTLCLsYr").val();
hour1= $("#tMTLCLs_HOUR").val();
min1= $("#tMTLCLs_MIN").val();
var Date1=month1+ "/" + day1+ "/" + year1+ " " + hour1 + ":" + min1+ ": 00" ;
day2= $("#tMTLCLsDay").val();
month2= $("#tMTLCLsMnth").val();
year2= $("#tMTLCLsYr").val();
hour3= $("#tMTLCLs_HOUR").val();
hour2= $("#tMTLCLs_MIN").val();
var Date2=month2+ "/" + day2+ "/" + year2+ " " + hour2 + ":" + hour2 + ": 00" ;
if(Date.parse(Date1)<Date.parse(Date2))
{
alert("Date 2 is large");
}