I need to Using a Date() utc , to round a time to the nearest five minutes then push it as array
example:
[
"2019_10_9_00_05",
"2019_10_9_00_10",
"2019_10_9_00_15",
"2019_10_9_00_20",
]
Code
const now = moment();
const time= [];
for (let i = 0; i < 4; i++) {
now.add(5, 'minutes');
time.push(now.utcOffset(-1).format('YYYY_M_DD_HH_mm'));
}
console.log(time);
output
[
"2019_10_9_00_03",
"2019_10_9_00_7",
"2019_10_9_00_11",
"2019_10_9_00_16",
]
any solution's please
You need to check if minutes value is multiple of 5 first.
const now = moment();
const factor = 5;
const time = [];
const minutes = now.minutes();
if (minutes !== 0 && minutes % factor) { // Check if there is a remainder
const remainder = minutes % factor; // Get remainder
now.add(factor - remainder, 'minutes'); // Update minutes value to nearest 5
}
for (let i = 0; i < 4; i++) {
time.push(now.utcOffset(-1).format('YYYY_M_DD_HH_mm'));
now.add(5, 'minutes');
}
console.log(time);
Result:
now: 2019_10_11_09_19
["2019_10_10_21_20", "2019_10_10_21_25", "2019_10_10_21_30", "2019_10_10_21_35"]