如何在 JavaScript 中减去日期/时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4944750/
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
How to subtract date/time in JavaScript?
提问by Ahmad Farid
I have a field at a grid containing date/time and I need to know the difference between that and the current date/time. What could be the best way of doing so?
我在包含日期/时间的网格中有一个字段,我需要知道它与当前日期/时间之间的区别。这样做的最佳方法是什么?
The dates are stored like "2011-02-07 15:13:06"
.
日期存储为"2011-02-07 15:13:06"
.
回答by dxh
This will give you the difference between two dates, in milliseconds
这将为您提供两个日期之间的差异,以毫秒为单位
var diff = Math.abs(date1 - date2);
In your example, it'd be
在你的例子中,它会是
var diff = Math.abs(new Date() - compareDate);
You need to make sure that compareDate
is a valid Date
object.
您需要确保这compareDate
是一个有效的Date
对象。
Something like this will probably work for you
像这样的东西可能对你有用
var diff = Math.abs(new Date() - new Date(dateStr.replace(/-/g,'/')));
i.e. turning "2011-02-07 15:13:06"
into new Date('2011/02/07 15:13:06')
, which is a format the Date
constructor can comprehend.
即"2011-02-07 15:13:06"
变成new Date('2011/02/07 15:13:06')
,这是Date
构造函数可以理解的格式。
回答by pawel
You can just substract two date objects.
您可以减去两个日期对象。
var d1 = new Date(); //"now"
var d2 = new Date("2011/02/01") // some date
var diff = Math.abs(d1-d2); // difference in milliseconds
回答by Shital Shah
Unless you are subtracting dates on same browser client and don't care about edge cases like day light saving time changes, you are probably better off using moment.jswhich offers powerful localized APIs. For example, this is what I have in my utils.js:
除非您在同一浏览器客户端上减去日期并且不关心夏令时更改等边缘情况,否则您最好使用提供强大本地化 API 的moment.js。例如,这是我在 utils.js 中的内容:
subtractDates: function(date1, date2) {
return moment.subtract(date1, date2).milliseconds();
},
millisecondsSince: function(dateSince) {
return moment().subtract(dateSince).milliseconds();
},
回答by Oleg
You can use getTime()
method to convert the Date
to the number of milliseconds since January 1, 1970. Then you can easy do any arithmetic operations with the dates. Of course you can convert the number back to the Date
with setTime()
. See herean example.
您可以使用getTime()
method 将 转换Date
为自 1970 年 1 月 1 日以来的毫秒数。然后您可以轻松地对日期进行任何算术运算。当然,您可以将数字转换回Date
with setTime()
。请参阅此处的示例。
回答by Pavel Savara
If you wish to get difference in wall clock time, for local timezone and with day-light saving awareness.
如果您希望获得挂钟时间、本地时区和夏令时意识的差异。
Date.prototype.diffDays = function (date: Date): number {
var utcThis = Date.UTC(this.getFullYear(), this.getMonth(), this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds(), this.getMilliseconds());
var utcOther = Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds());
return (utcThis - utcOther) / 86400000;
};
Test
测试
it('diffDays - Czech DST', function () {
// expect this to parse as local time
// with Czech calendar DST change happened 2012-03-25 02:00
var pre = new Date('2012/03/24 03:04:05');
var post = new Date('2012/03/27 03:04:05');
// regardless DST, you still wish to see 3 days
expect(pre.diffDays(post)).toEqual(-3);
});
Diff minutes or seconds is in same fashion.
Diff 分钟或秒的方式相同。