Javascript:如何将 n 分钟添加到 unix 时间戳
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16240313/
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
Javascript: how to add n minutes to unix timestamp
提问by iamjonesy
I have a unix timestamp: 1368435600. And a duration in minutes: 75 for example.
我有一个 unix 时间戳:1368435600。以分钟为单位的持续时间:例如 75。
Using javascript I need to:
使用 javascript 我需要:
- Convert the timestamp to a string format hours:mins (09:00)
- Add n minutes to the timestamp: timestamp + 75mins
- 将时间戳转换为字符串格式 hours:mins (09:00)
- 给时间戳添加 n 分钟:timestamp + 75mins
I tried the moment.js library:
我尝试了 moment.js 库:
end_time = moment(start_time).add('m', booking_service_duration);
booking_service_duration was 75 but it added an hour. I'd also rather not have to use another js library
booking_service_duration 是 75,但它增加了一个小时。我也宁愿不必使用另一个 js 库
回答by nullability
To add 75 minutes, just multiply by 60 to get the number of seconds, and add that to the timestamp:
要添加 75 分钟,只需乘以 60 即可获得秒数,并将其添加到时间戳中:
timestamp += 75 * 60
To convert to hours:mins you will have to do a bit more math:
要转换为小时:分钟,您将需要做更多的数学运算:
var hours = Math.floor(timestamp/60/60),
mins = Math.floor((timestamp - hours * 60 * 60) / 60),
output = hours%24+":"+mins;
回答by Jean-Bernard Pellerin
Unix time is the number of seconds that have elapsed since 1 January 1970 UTC.
To move that time forward you simply add the number of seconds.
Unix 时间是自 UTC 时间 1970 年 1 月 1 日以来经过的秒数。
要向前移动该时间,您只需添加秒数。
So once you have the minutes, the new timestamp is oldTime + 60*minutes
For the conversion look up parsing libraries, there is code out there for this, do some research.
所以一旦你有时间,新的时间戳是oldTime + 60*minutes
转换查找解析库,有代码可以做一些研究。