Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
350 views
in Technique[技术] by (71.8m points)

javascript - 获取两个日期时间之间的时差(Get the time difference between two datetimes)

I know I can do anything and some more envolving Dates with momentjs.

(我知道我可以使用momentjs做任何事情,还可以做一些涉及日期的事情。)

But embarrassingly, I'm having a hard time trying to do something that seems simple: geting the difference between 2 times.

(但是令人尴尬的是,我很难去做一件看起来很简单的事情:得到两次之间的差。)

Example:

(例:)

var now  = "04/09/2013 15:00:00";
var then = "04/09/2013 14:20:30";

//expected result:
"00:39:30"

what I tried:

(我试过的)

var now = moment("04/09/2013 15:00:00");
var then = moment("04/09/2013 14:20:30");

console.log(moment(moment.duration(now.diff(then))).format("hh:mm:ss"))
//outputs 10:39:30  

I do not understand what is that "10" there.

(我不明白那里的“ 10”是什么。)

I live in Brazil, so we are utc-0300 if that is relevant.

(我住在巴西,所以如果相关的话,我们是utc-0300。)

The result of moment.duration(now.diff(then)) is a duration with the correct internal values:

(moment.duration(now.diff(then))是具有正确内部值的持续时间:)

 days: 0
 hours: 0
 milliseconds: 0
 minutes: 39
 months: 0
 seconds: 30
 years: 0

So, I guess my question is: how to convert a momentjs Duration to a time interval?

(因此,我想我的问题是:如何将momentjs持续时间转换为时间间隔?)

I sure can use

(我肯定可以用)

duration.get("hours") +":"+ duration.get("minutes") +:+ duration.get("seconds")

but i feel that there is something more elegant that I am completely missing.

(但是我觉得我完全没有什么更优雅的东西。)

update

(更新)


looking closer, in the above example now is:

(仔细观察,在上面的示例中now是:)

Tue Apr 09 2013 15:00:00 GMT-0300 (E. South America Standard Time)…}

and moment(moment.duration(now.diff(then))) is:

(而moment(moment.duration(now.diff(then)))为:)

Wed Dec 31 1969 22:39:30 GMT-0200 (E. South America Daylight Time)…}

I am not sure why the second value is in Daylight Time (-0200)... but I am sure that i do not like dates :(

(我不确定为什么第二个值是夏令时(-0200)...但是我确定我不喜欢日期:()

update 2

(更新2)

well, the value is -0200 probably because 31/12/1969 was a date where the daylight time was being used... so thats that.

(好吧,该值为-0200,可能是因为1969年12月31日是使用夏令时的日期。)

  ask by Leo translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This approach will work ONLY when the total duration is less than 24 hours:

(此方法仅在总持续时间少于24小时时有效:)

var now  = "04/09/2013 15:00:00";
var then = "04/09/2013 14:20:30";

moment.utc(moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss"))).format("HH:mm:ss")

// outputs: "00:39:30"

If you have 24 hours or more, the hours will reset to zero with the above approach, so it is not ideal.

(如果您有24小时或更长时间,则上述方法会将小时数重置为零,因此并不理想。)

If you want to get a valid response for durations of 24 hours or greater, then you'll have to do something like this instead:

(如果您想获得24小时或更长时间的有效回复,则必须执行以下操作:)

var now  = "04/09/2013 15:00:00";
var then = "02/09/2013 14:20:30";

var ms = moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss"));
var d = moment.duration(ms);
var s = Math.floor(d.asHours()) + moment.utc(ms).format(":mm:ss");

// outputs: "48:39:30"

Note that I'm using the utc time as a shortcut.

(请注意,我使用utc时间作为快捷方式。)

You could pull out d.minutes() and d.seconds() separately, but you would also have to zeropad them.

(您可以分别拉出d.minutes()d.seconds() ,但也必须对它们进行零填充。)

This is necessary because the ability to format a duration objection is not currently in moment.js.

(这是必需的,因为moment.js当前尚无格式化duration异议的功能。)

It has been requested here .

(在这里已被要求 。)

However, there is a third-party plugin called moment-duration-format that is specifically for this purpose:

(但是,有一个专门用于此目的的第三方插件称为moment-duration-format :)

var now  = "04/09/2013 15:00:00";
var then = "02/09/2013 14:20:30";

var ms = moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss"));
var d = moment.duration(ms);
var s = d.format("hh:mm:ss");

// outputs: "48:39:30"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...