Javascript:如何将n分钟添加到unix时间戳 [英] Javascript: how to add n minutes to unix timestamp
本文介绍了Javascript:如何将n分钟添加到unix时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个unix时间戳:1368435600。持续时间以分钟为单位:例如75.
I have a unix timestamp: 1368435600. And a duration in minutes: 75 for example.
使用javascript我需要:
Using javascript I need to:
- 将时间戳转换为字符串格式小时:分钟(09:00)
- 将n分钟添加到时间戳:timestamp + 75mins
我试过了moment.js库:
I tried the moment.js library:
end_time = moment(start_time).add('m', booking_service_duration);
booking_service_duration为75但是增加了一个小时。我也不想使用另一个js库
booking_service_duration was 75 but it added an hour. I'd also rather not have to use another js library
推荐答案
要添加75分钟,只需乘以60得到数字秒,并将其添加到时间戳:
To add 75 minutes, just multiply by 60 to get the number of seconds, and add that to the timestamp:
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;
这篇关于Javascript:如何将n分钟添加到unix时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文