如何在javascript中减去日期/时间? [重复]

时间:2022-10-20 16:30:11

This question already has an answer here:

这个问题在这里已有答案:

I have a field at a grid containing date/time and I need to know the difference between that and current date/time. What could be the best way of doing so?

我在包含日期/时间的网格中有一个字段,我需要知道它与当前日期/时间之间的差异。这可能是最好的方式吗?

5 个解决方案

#1


171  

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”转换为新日期('2011/02/07 15:13:06'),这是Date构造函数可以理解的格式。

#2


22  

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

#3


11  

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.js which 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();
},

#4


7  

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 here an example.

您可以使用getTime()方法将Date转换为自1970年1月1日以来的毫秒数。然后您可以轻松地对日期进行任何算术运算。当然,您可以使用setTime()将数字转换回Date。看这里的例子。

#5


6  

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.

差分或秒以相同的方式。

#1


171  

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”转换为新日期('2011/02/07 15:13:06'),这是Date构造函数可以理解的格式。

#2


22  

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

#3


11  

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.js which 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();
},

#4


7  

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 here an example.

您可以使用getTime()方法将Date转换为自1970年1月1日以来的毫秒数。然后您可以轻松地对日期进行任何算术运算。当然,您可以使用setTime()将数字转换回Date。看这里的例子。

#5


6  

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.

差分或秒以相同的方式。