gpt4 book ai didi

javascript - 如何以数组形式获取 5 分钟间隔内的最后几个小时 - MySQL 时间戳格式?

转载 作者:行者123 更新时间:2023-12-01 01:00:07 25 4
gpt4 key购买 nike

我做到了

var date, lastTen = [];
date = new Date();

while (date.getMinutes() % interval !== 0) {
date.setMinutes ( date.getMinutes() + 1 );
}

for (var i = 0; i < 24 * (60/interval); i++) {
lastTen.push(date.getHours() + ':' + date.getMinutes());
date.setMinutes ( date.getMinutes() - interval);
}

console.log(lastTen.slice(0,10));

我得到

(10) ["17:5", "17:0", "16:55", "16:50", "16:45", "16:40", "16:35", "16:30", "16:25", "16:20"]

如何将这些小时字符串转换为 mysql 格式?

["2019-05-15 17:5:00", "2019-05-15 17:0:00", "2019-05-15 16:55:00", "2019-05-15 16:50:00", "2019-05-15 16:45:00", "2019-05-15 16:40:00", "2019-05-15 16:35:00", "2019-05-15 16:30:00", "2019-05-15 16:25:00", "2019-05-15 16:20"]

最佳答案

可以说,您只是“错误”地打印了它。您需要在小时、分钟和秒之前添加 0。你还需要几秒钟

var date, lastTen = [];
date = new Date();

while (date.getMinutes() % interval !== 0) {
date.setMinutes ( date.getMinutes() + 1 );
}

for (var i = 0; i < 24 * (60/interval); i++) {
lastTen.push(date.getFullYear()+"-"+date.getMonth()+"-"+date.getDate() + " " + (date.getHours()<10?'0':'') + date.getHours() + ':' + (date.getMinutes()<10?'0':'') + date.getMinutes() + ":" + (date.getSeconds()<10?'0':'') + date.getSeconds());
date.setMinutes ( date.getMinutes() - interval);
}

console.log(lastTen.slice(0,10));

关于javascript - 如何以数组形式获取 5 分钟间隔内的最后几个小时 - MySQL 时间戳格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56157652/

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