使用Javascript中的时间跨度

Chr*_*row 22 javascript datetime timespan date datejs

已经使用Date.js,但如果需要也可以使用其他库.

不确定使用时间增量的最佳方法是什么.具体来说,我想显示从现在到过去的日期时间之间经过的时间.

所以我需要做这样的事情:

var elapsed_time = new Date() - pastDate;
pastDate.toString('days-hours-minutes-seconds');
Run Code Online (Sandbox Code Playgroud)

得到它主要使用Date.js工作,但问题是现在我正在使用Date对象而不是时间跨度,所以应该是23小时的时间跨度是在Date的第一次之后23小时:

var result = (new Date()) - past_date;
"result" is the number (probably milliseconds): 15452732
var result = (new Date() - past_date
"result" is a date from 1969: Wed Dec 31 1969 23:17:32

我需要的是:

0 days 23 hours 17 minutes and 32 seconds

有任何想法吗?

Lee*_*lor 56

听起来你需要moment.js

例如

moment().subtract('days', 6).calendar();
Run Code Online (Sandbox Code Playgroud)

=>上周日晚上8:23

moment().startOf('hour').fromNow();
Run Code Online (Sandbox Code Playgroud)

=> 26分钟前

编辑:

纯JS日期差异计算:

var date1 = new Date("7/Nov/2012 20:30:00");
var date2 = new Date("20/Nov/2012 19:15:00");

var diff = date2.getTime() - date1.getTime();

var days = Math.floor(diff / (1000 * 60 * 60 * 24));
diff -=  days * (1000 * 60 * 60 * 24);

var hours = Math.floor(diff / (1000 * 60 * 60));
diff -= hours * (1000 * 60 * 60);

var mins = Math.floor(diff / (1000 * 60));
diff -= mins * (1000 * 60);

var seconds = Math.floor(diff / (1000));
diff -= seconds * (1000);

document.write(days + " days, " + hours + " hours, " + mins + " minutes, " + seconds + " seconds");
Run Code Online (Sandbox Code Playgroud)

  • 更新 2020.10 #1:来自 moment.js 文档:“在大多数情况下,您不应该为新项目选择 Moment” - https://momentjs.com/docs/ (5认同)
  • 是的,还有另一个人看着它.即使使用moment.js,时间跨度上也没有足够的"格式"选项.我们必须这样做的方式是手动,就像你上面一样. (2认同)

syd*_*uki 9

这是一个简单的时间戳格式化程序,具有自定义模式支持区域设置感知,使用Intl.RelativeTimeFormat

格式化示例:

/** delta: 1234567890, @locale: 'en-US', @style: 'long' */

/* D~ h~ m~ s~ f~ */
14 days 6 hours 56 minutes 7 seconds 890

/* D#"d" h#"h" m#"m" s#"s" f#"ms" */
14d 6h 56m 7s 890ms

/* D~, h:m:s.f */
14 days, 06:56:07.890

/* h~ m~ s~ */
342 hours 56 minutes 7 seconds
   
/* up D~, h:m */
up 14 days, 06:56
Run Code Online (Sandbox Code Playgroud)

使用说明、源代码和实例如下:

/** delta: 1234567890, @locale: 'en-US', @style: 'long' */

/* D~ h~ m~ s~ f~ */
14 days 6 hours 56 minutes 7 seconds 890

/* D#"d" h#"h" m#"m" s#"s" f#"ms" */
14d 6h 56m 7s 890ms

/* D~, h:m:s.f */
14 days, 06:56:07.890

/* h~ m~ s~ */
342 hours 56 minutes 7 seconds
   
/* up D~, h:m */
up 14 days, 06:56
Run Code Online (Sandbox Code Playgroud)


Pau*_* S. 7

如果你在几天后对准确性不太担心,你可以简单地做数学

function timeSince(when) { // this ignores months
    var obj = {};
    obj._milliseconds = (new Date()).valueOf() - when.valueOf();
    obj.milliseconds = obj._milliseconds % 1000;
    obj._seconds = (obj._milliseconds - obj.milliseconds) / 1000;
    obj.seconds = obj._seconds % 60;
    obj._minutes = (obj._seconds - obj.seconds) / 60;
    obj.minutes = obj._minutes % 60;
    obj._hours = (obj._minutes - obj.minutes) / 60;
    obj.hours = obj._hours % 24;
    obj._days = (obj._hours - obj.hours) / 24;
    obj.days = obj._days % 365;
    // finally
    obj.years = (obj._days - obj.days) / 365;
    return obj;
}
Run Code Online (Sandbox Code Playgroud)

然后根据需要timeSince(pastDate);使用属性.

否则你可以.getUTC*用来计算它,但请注意它的计算速度可能稍慢

function timeSince(then) {
    var now = new Date(), obj = {};
    obj.milliseconds = now.getUTCMilliseconds() - then.getUTCMilliseconds();
    obj.seconds = now.getUTCSeconds() - then.getUTCSeconds();
    obj.minutes = now.getUTCMinutes() - then.getUTCMinutes();
    obj.hours = now.getUTCHours() - then.getUTCHours();
    obj.days = now.getUTCDate() - then.getUTCDate();
    obj.months = now.getUTCMonth() - then.getUTCMonth();
    obj.years = now.getUTCFullYear() - then.getUTCFullYear();
    // fix negatives
    if (obj.milliseconds < 0) --obj.seconds, obj.milliseconds = (obj.milliseconds + 1000) % 1000;
    if (obj.seconds < 0) --obj.minutes, obj.seconds = (obj.seconds + 60) % 60;
    if (obj.minutes < 0) --obj.hours, obj.minutes = (obj.minutes + 60) % 60;
    if (obj.hours < 0) --obj.days, obj.hours = (obj.hours + 24) % 24;
    if (obj.days < 0) { // months have different lengths
        --obj.months;
        now.setUTCMonth(now.getUTCMonth() + 1);
        now.setUTCDate(0);
        obj.days = (obj.days + now.getUTCDate()) % now.getUTCDate();
    }
    if (obj.months < 0)  --obj.years, obj.months = (obj.months + 12) % 12;
    return obj;
}
Run Code Online (Sandbox Code Playgroud)


Vla*_*den 6

您可以使用momentjs的 持续时间对象

例:

const diff = moment.duration(Date.now() - new Date(2010, 1, 1))
console.log(`${diff.years()} years ${diff.months()} months ${diff.days()} days ${diff.hours()} hours ${diff.minutes()} minutes and ${diff.seconds()} seconds`)
Run Code Online (Sandbox Code Playgroud)