gpt4 book ai didi

javascript - 将时间分成 30 分钟的小块

转载 作者:行者123 更新时间:2023-11-29 16:52:21 25 4
gpt4 key购买 nike

我有两个日期开始和结束

"start" : "Wed Jan 27 2016 21:00:00 GMT-0500",
"end" : "Thu Jan 27 2016 23:30:00 GMT-0500"

我想把这段时间分成如下几 block

"start" : "Wed Jan 27 2016 21:00:00 GMT-0500",
"end" : "Thu Jan 27 2016 21:30:00 GMT-0500"
----------------------------------------------
"start" : "Wed Jan 27 2016 21:30:00 GMT-0500",
"end" : "Thu Jan 27 2016 22:00:00 GMT-0500"
----------------------------------------------
"start" : "Wed Jan 27 2016 22:00:00 GMT-0500",
"end" : "Thu Jan 27 2016 22:30:00 GMT-0500"
----------------------------------------------
"start" : "Wed Jan 27 2016 22:30:00 GMT-0500",
"end" : "Thu Jan 27 2016 23:00:00 GMT-0500"
----------------------------------------------
"start" : "Wed Jan 27 2016 23:00:00 GMT-0500",
"end" : "Thu Jan 27 2016 23:30:00 GMT-0500"

有没有办法使用 moment.js 或 javascript 来做到这一点?

最佳答案

我们可以像这样把时间分成几 block :

 var time = {
"start": "Wed Jan 27 2016 21:00:00 GMT-0500",
"end": "Thu Jan 27 2016 23:30:00 GMT-0500"
};


var startTime = new Date(time.start).getTime();
var endTime = new Date(time.end).getTime();

var diff = endTime - startTime;

var chunks = [];
var hold = startTime;
var threshold = (60 * 30 * 1000); //30minutes
for (var i = (startTime + threshold); i <= endTime; i += (threshold)) {
var newEndTime = new Date(i);
chunks.push({
start: new Date(hold),
end: newEndTime
});
hold = newEndTime;
}
console.log(chunks)

关于javascript - 将时间分成 30 分钟的小块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35053665/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com