0

即使我在当前季度,我也需要将我的时间四舍五入。这是我的代码:

const currentDate = new Date(2020, 1, 8, 9, 42, 0, 0);
let roundedUpQuarter = Math.ceil(currentDate.getMinutes() / 15) * 15 % 60;

所以在我的例子中,当前时间09:42在我的roundedUpQuarter变量中我会得到很好的结果09:45 但是当我在当前时间发送 0、15、30 或 45 分钟时遇到问题,因为我也需要四舍五入。

例如,如果我现在的时间是09:30我需要得到09:45

我不想if condition用来做那个。仅使用公式可以做到这一点吗?

4

2 回答 2

3
const currentDate = new Date(2020, 1, 8, 9, 42, 0, 0);
let roundedUpQuarter = Math.ceil((currentDate.getMinutes()+0.1) / 15) * 15 % 60;
于 2021-01-08T11:01:21.397 回答
0

检查% 15 === 0并将 1 分钟添加到实际分钟数。然后继续你的方程式。

const currentDate = new Date(2020, 1, 8, 9, 45, 0, 0);
var minutes = currentDate.getMinutes() % 15 === 0 ? (currentDate.getMinutes() + 1) : currentDate.getMinutes();
let roundedUpQuarter = Math.ceil(minutes / 15) * 15 % 60;

console.log(roundedUpQuarter); // for 45 return 0
于 2021-01-08T10:58:04.950 回答