我可以使用 Luxon 将 DateTime 与时间字符串结合起来吗?
Can I use Luxon to combine a DateTime with a time string?
我有一个 TimePicker
组件 returns 24 小时制,格式如下:09:00
表示上午 9 点或 12:00
表示中午 12 点或 [=14] =] 晚上 8 点。我的代码中的某些内容需要 Date
(JSDate),所以我只想获取当前的 date/time DateTime.now()
,但应用 TimePicker
组件中的小时和分钟,该组件具有 onClick
事件。我可以像这样处理该事件:
// TimePickerValue can either be a string or JSDate
// but the TimePicker is always returning a string
const handleTimeChange = (time:TimePickerValue) => {
// this outputs a time in 24-hour format
console.log("handleTimeChange: ", time)
// I want to set the state of the parent component
// this needs to be in JSDate format so this doesn't work
// setSomeValue(time)
// Because I don't care about date, I can just use "now()" and convert
// it to a JSDate but I want the hours and minutes of the value I'm setting
// to be the time in the string being sent to this function.
setSomeValue(DateTime.now().toJSDate())
}
Luxon 能否解析“13:00”之类的内容或将其应用到现有的 DateTime
以便覆盖其现有的 hours
和 minutes
?
Can Luxon parse something like "13:00"
是的,you can just use the fromISO
method to parse a time string。
const parsed = DateTime.fromISO('20:00');
console.log(parsed.toString()); // 2021-04-07T20:00:00.000+10:00
Can Luxon apply it to an existing DateTime so it writes over its existing hours and minutes?
这可能有点难,我不知道 Luxon 中是否有“内置”方式来做到这一点。但是,如果您使用 fromISO
解析时间字符串,它会将日期部分设置为“今天”,因此您可以使用 diff
计算出“一天中的时间”(作为 Duration
),然后使用它来设置您其他日期的时间:
const parsed = DateTime.fromISO(time);
const today = DateTime.now().startOf('day');
const timeOfDay = parsed.diff(today);
const dateToModify = DateTime.fromJSDate(otherDate);
const result = dateToModify.startOf('day').plus(timeOfDay);
或者,如果您有时间的“部分”,您可以使用 Luxon's set
method 覆盖那些单独的部分:
const dateToModify = DateTime.fromJSDate(otherDate);
// Not sure what's in the 'TimePickerValue' object but if you had hours and minutes:
const result = dateToModify.set({
hours: time.hours,
minutes: time.minutes,
});
我有一个 TimePicker
组件 returns 24 小时制,格式如下:09:00
表示上午 9 点或 12:00
表示中午 12 点或 [=14] =] 晚上 8 点。我的代码中的某些内容需要 Date
(JSDate),所以我只想获取当前的 date/time DateTime.now()
,但应用 TimePicker
组件中的小时和分钟,该组件具有 onClick
事件。我可以像这样处理该事件:
// TimePickerValue can either be a string or JSDate
// but the TimePicker is always returning a string
const handleTimeChange = (time:TimePickerValue) => {
// this outputs a time in 24-hour format
console.log("handleTimeChange: ", time)
// I want to set the state of the parent component
// this needs to be in JSDate format so this doesn't work
// setSomeValue(time)
// Because I don't care about date, I can just use "now()" and convert
// it to a JSDate but I want the hours and minutes of the value I'm setting
// to be the time in the string being sent to this function.
setSomeValue(DateTime.now().toJSDate())
}
Luxon 能否解析“13:00”之类的内容或将其应用到现有的 DateTime
以便覆盖其现有的 hours
和 minutes
?
Can Luxon parse something like "13:00"
是的,you can just use the fromISO
method to parse a time string。
const parsed = DateTime.fromISO('20:00');
console.log(parsed.toString()); // 2021-04-07T20:00:00.000+10:00
Can Luxon apply it to an existing DateTime so it writes over its existing hours and minutes?
这可能有点难,我不知道 Luxon 中是否有“内置”方式来做到这一点。但是,如果您使用 fromISO
解析时间字符串,它会将日期部分设置为“今天”,因此您可以使用 diff
计算出“一天中的时间”(作为 Duration
),然后使用它来设置您其他日期的时间:
const parsed = DateTime.fromISO(time);
const today = DateTime.now().startOf('day');
const timeOfDay = parsed.diff(today);
const dateToModify = DateTime.fromJSDate(otherDate);
const result = dateToModify.startOf('day').plus(timeOfDay);
或者,如果您有时间的“部分”,您可以使用 Luxon's set
method 覆盖那些单独的部分:
const dateToModify = DateTime.fromJSDate(otherDate);
// Not sure what's in the 'TimePickerValue' object but if you had hours and minutes:
const result = dateToModify.set({
hours: time.hours,
minutes: time.minutes,
});