简体   繁体   English

我可以使用 Luxon 将 DateTime 与时间字符串结合起来吗?

[英]Can I use Luxon to combine a DateTime with a time string?

I've got a TimePicker component that returns a 24-hour time in this format: 09:00 for 9AM or 12:00 for 12PM or 20:00 for 8PM.我有一个TimePicker组件,它以这种格式返回 24 小时时间:上午 9 点的09:0012:00点的 12:00 或晚上 8 点的20:00 Something in my code requires a Date (JSDate) so I want to just take the current date/time DateTime.now() but apply the hours and minutes from the TimePicker component which has an onClick event.我的代码中的某些内容需要Date (JSDate),因此我只想获取当前日期/时间DateTime.now()但应用具有onClick事件的TimePicker组件的小时和分钟。 I can handle that event like so:我可以像这样处理那个事件:

// 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())
}

Can Luxon parse something like "13:00" or apply it an existing DateTime so it writes over its existing hours and minutes ? Luxon 是否可以解析“13:00”之类的内容或将其应用到现有的DateTime以便它写入现有的hoursminutes

Can Luxon parse something like "13:00" Luxon 可以解析类似“13:00”的内容吗

Yes, you can just use the fromISO method to parse a time string .是的,您可以只使用fromISO方法来解析时间字符串

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 能否将它应用到现有的 DateTime 以便它写入现有的小时和分钟?

This could be a bit harder, I don't know if there's a "built-in" way in Luxon to do that.这可能有点困难,我不知道 Luxon 中是否有“内置”方式来做到这一点。 But, if you parse the time string using fromISO , it will set the date part to "today", so you could use diff to work out the "time of day" (as a Duration ) and then use that to set the time of day on your other date:但是,如果您使用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);

Alternatively, if you have the "parts" of the time, you can use Luxon's set method to overwrite those individual parts:或者,如果您有时间的“部分”,您可以使用Luxon 的set方法覆盖这些单独的部分:

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,
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM