简体   繁体   English

用日期计算24小时格式的时差

[英]Calculate time difference in 24 Hours format with date

I'm working on Airline API. 我正在使用Airline API。 I came across a situation where I have to calculate the flying time of the airline. 我遇到一种情况,必须计算航空公司的飞行时间。

Standard Departure Time is 2014-10-20 22:30:00

Standard Arrival Time is >2014-10-21 06:00:00

The flight departs by 10:30 PM on Oct 20 2014 and arrives at 6:00 AM the next day. 航班于2014年10月20日晚上10:30出发,于第二天上午6:00到达。

Is there a working snippet? 有工作片段吗?

You can calculate time differences if you convert your dates to timestamps, which are int values. 如果将日期转换为int值的时间戳,则可以计算时差。

pseudocode example: timestampDiff = dateToTimestamp(date2) - dateToTimeStamp(date1) 伪代码示例:timestampDiff = dateToTimestamp(date2)-dateToTimeStamp(date1)

in javascript: 在javascript中:

var timestampDiff = new Date("2014-10-21 06:00:00").getTime() - new Date("2014-10-20 22:30:00").getTime();
var diff = new Date(timestampDiff);

diff is now a Date, you can get the hours, minutes, seconds like this: diff现在是一个日期,您可以像这样获得小时,分钟,秒:

diff.getHours();
diff.getMinutes();
diff.getSeconds();

hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM