如何使用 Luxon 以分钟为单位划分小时间隔?例子
starTime = 06:00
endaTime = 12:00
interval = 00:30
["06:00AM", "06:30AM", "07:00AM", "07:30AM", "08:00AM", "08:30AM", "09:00AM", "09:30AM", "10:00AM", "10:30AM", "11:00AM", "11:30AM"]
如何使用 Luxon 以分钟为单位划分小时间隔?例子
starTime = 06:00
endaTime = 12:00
interval = 00:30
["06:00AM", "06:30AM", "07:00AM", "07:30AM", "08:00AM", "08:30AM", "09:00AM", "09:30AM", "10:00AM", "10:30AM", "11:00AM", "11:30AM"]
您可以简单地:
DateTime
开始和结束时间的对象DateTime.fromFormat
Duration
对象Duration.fromISOTime
plus
)例子:
const DateTime = luxon.DateTime;
const Duration = luxon.Duration;
const startTime = "06:00"
const endTime = "12:00"
const interval = "00:30"
const dtStart = DateTime.fromFormat(startTime, "HH:mm");
const dtEnd = DateTime.fromFormat(endTime, "HH:mm");
const durationInterval = Duration.fromISOTime(interval);
let res = [];
let i = DateTime.fromFormat(startTime, "HH:mm");
while (i < dtEnd) {
res.push(i.toFormat("HH:mma"));
i = i.plus(durationInterval);
}
console.log(res);
<script src="https://cdn.jsdelivr.net/npm/luxon@1.26.0/build/global/luxon.js"></script>