我做了这个

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/

10-15 15:10