用 Luxon 以分钟为单位划分小时间隔
Divide hours interval in minutes with Luxon
我如何用 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
object for start and end time using DateTime.fromFormat
- 创建
Duration
object for your "interval" using Duration.fromISOTime
- 循环直到到达结束时间,在每次交互时添加间隔(参见 Comparing DateTimes and
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>
我如何用 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
object for start and end time usingDateTime.fromFormat
- 创建
Duration
object for your "interval" usingDuration.fromISOTime
- 循环直到到达结束时间,在每次交互时添加间隔(参见 Comparing DateTimes and
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>