I want to generate this data list (00:00 ~ 24:00, every 3 minutes)
[ "00:00", "00:03", "00:06", "00:09", "00:12", "00:15", "00:18", "00:21", "00:24", "00:27", "00:30", "00:33", "00:36", "00:39", "00:42", "00:45", "00:48", "00:51", "00:54", "00:57", "01:00", "01:03", "01:06", "01:09", "01:12", "01:15", "01:18", "01:21", "01:24", "01:27", "01:30", "01:33", "01:36", "01:39", "01:42", "01:45", "01:48", "01:51", "01:54", "01:57", "02:00", "02:03", "02:06", "02:09", "02:12", "02:15", "02:18", "02:21", "02:24", "02:27", "02:30", "02:33", "02:36", "02:39", "02:42", "02:45", "02:48", "02:51", "02:54", "02:57", "03:00", "03:03", "03:06", "03:09", "03:12", "03:15", "03:18", "03:21", "03:24", "03:27", "03:30", "03:33", "03:36", "03:39", "03:42", "03:45", "03:48", "03:51", "03:54", "03:57", "04:00", "04:03", "04:06", "04:09", "04:12", "04:15", "04:18", "04:21", "04:24", "04:27", "04:30", "04:33", "04:36", "04:39", "04:42", "04:45", "04:48", "04:51", "04:54", "04:57", "05:00", "05:03", "05:06", "05:09", "05:12", "05:15", "05:18", "05:21", "05:24", "05:27", "05:30", "05:33", "05:36", "05:39", "05:42", "05:45", "05:48", "05:51", "05:54", "05:57", "06:00", "06:03", "06:06", "06:09", "06:12", "06:15", "06:18", "06:21", "06:24", "06:27", "06:30", "06:33", "06:36", "06:39", "06:42", "06:45", "06:48", "06:51", "06:54", "06:57", "07:00", "07:03", "07:06", "07:09", "07:12", "07:15", "07:18", "07:21", "07:24", "07:27", "07:30", "07:33", "07:36", "07:39", "07:42", "07:45", "07:48", "07:51", "07:54", "07:57", "08:00", "08:03", "08:06", "08:09", "08:12", "08:15", "08:18", "08:21", "08:24", "08:27", "08:30", "08:33", "08:36", "08:39", "08:42", "08:45", "08:48", "08:51", "08:54", "08:57", "09:00", "09:03", "09:06", "09:09", "09:12", "09:15", "09:18", "09:21", "09:24", "09:27", "09:30", "09:33", "09:36", "09:39", "09:42", "09:45", "09:48", "09:51", "09:54", "09:57", "10:00", "10:03", "10:06", "10:09", "10:12", "10:15", "10:18", "10:21", "10:24", "10:27", "10:30", "10:33", "10:36", "10:39", "10:42", "10:45", "10:48", "10:51", "10:54", "10:57", "11:00", "11:03", "11:06", "11:09", "11:12", "11:15", "11:18", "11:21", "11:24", "11:27", "11:30", "11:33", "11:36", "11:39", "11:42", "11:45", "11:48", "11:51", "11:54", "11:57", "12:00", "12:03", "12:06", "12:09", "12:12", "12:15", "12:18", "12:21", "12:24", "12:27", "12:30", "12:33", "12:36", "12:39", "12:42", "12:45", "12:48", "12:51", "12:54", "12:57", "13:00", "13:03", "13:06", "13:09", "13:12", "13:15", "13:18", "13:21", "13:24", "13:27", "13:30", "13:33", "13:36", "13:39", "13:42", "13:45", "13:48", "13:51", "13:54", "13:57", "14:00", "14:03", "14:06", "14:09", "14:12", "14:15", "14:18", "14:21", "14:24", "14:27", "14:30", "14:33", "14:36", "14:39", "14:42", "14:45", "14:48", "14:51", "14:54", "14:57", "15:00", "15:03", "15:06", "15:09", "15:12", "15:15", "15:18", "15:21", "15:24", "15:27", "15:30", "15:33", "15:36", "15:39", "15:42", "15:45", "15:48", "15:51", "15:54", "15:57", "16:00", "16:03", "16:06", "16:09", "16:12", "16:15", "16:18", "16:21", "16:24", "16:27", "16:30", "16:33", "16:36", "16:39", "16:42", "16:45", "16:48", "16:51", "16:54", "16:57", "17:00", "17:03", "17:06", "17:09", "17:12", "17:15", "17:18", "17:21", "17:24", "17:27", "17:30", "17:33", "17:36", "17:39", "17:42", "17:45", "17:48", "17:51", "17:54", "17:57", "18:00", "18:03", "18:06", "18:09", "18:12", "18:15", "18:18", "18:21", "18:24", "18:27", "18:30", "18:33", "18:36", "18:39", "18:42", "18:45", "18:48", "18:51", "18:54", "18:57", "19:00", "19:03", "19:06", "19:09", "19:12", "19:15", "19:18", "19:21", "19:24", "19:27", "19:30", "19:33", "19:36", "19:39", "19:42", "19:45", "19:48", "19:51", "19:54", "19:57", "20:00", "20:03", "20:06", "20:09", "20:12", "20:15", "20:18", "20:21", "20:24", "20:27", "20:30", "20:33", "20:36", "20:39", "20:42", "20:45", "20:48", "20:51", "20:54", "20:57", "21:00", "21:03", "21:06", "21:09", "21:12", "21:15", "21:18", "21:21", "21:24", "21:27", "21:30", "21:33", "21:36", "21:39", "21:42", "21:45", "21:48", "21:51", "21:54", "21:57", "22:00", "22:03", "22:06", "22:09", "22:12", "22:15", "22:18", "22:21", "22:24", "22:27", "22:30", "22:33", "22:36", "22:39", "22:42", "22:45", "22:48", "22:51", "22:54", "22:57", "23:00", "23:03", "23:06", "23:09", "23:12", "23:15", "23:18", "23:21", "23:24", "23:27", "23:30", "23:33", "23:36", "23:39", "23:42", "23:45", "23:48", "23:51", "23:54", "23:57", "24:00" ],
I try to use for loop to generate "hour" first:
let arr = []
for (let i = 0; i < 25; i ) {
let j = 19
let str = i.toString()
arr[j*i] = str.padStart(2, '0') ':00'
}
console.log('arr', arr)
but I don't know how to insert minutes like 00:03 ~ 00:57 in the array...
CodePudding user response:
You can use a loop to generate all times with an interval of 3 minutes.
const result = [];
const interval = 3;
for(let i = 0; i <= 60 * 24; i = interval) {
let hours = Math.floor(i / 60);
let minutes = i % 60;
result.push(`${String(hours).padStart(2, 0)}:${String(minutes).padStart(2,0)}`);
}
console.log(result);
CodePudding user response:
You can just loop over your hour and minute values, pushing them to the array in the appropriate format as you go:
arr = []
interval = 3
for (let hr = 0; hr < 24; hr ) {
for (let min = 0; min < 60; min = interval) {
arr.push(`${String(hr).padStart(2, '0')}:${String(min).padStart(2, '0')}`)
}
}
arr.push('24:00')
console.log(arr)
CodePudding user response:
let arr = [];
for (let i = 0; i < 25; i ) {
for (let j = 0; j < 60; j = j 3) {
if (i < 10) {
arr.push(`0${i}:${j}`);
} else {
arr.push(`${i}:${j}`);
}
}
}
console.log(arr);