简体   繁体   English

仅使用 Luxon 格式化时间

[英]Format time only with Luxon

I have a time saved as a string in my database.我有一个时间保存为我的数据库中的字符串。 I'm trying to parse that time using Luxon, and then set various date parts to my various controls.我正在尝试使用 Luxon 解析那个时间,然后为我的各种控件设置各种日期部分。 While I'm not getting any errors during the parsing, I'm getting unexpected values.虽然我在解析过程中没有收到任何错误,但我得到了意想不到的值。

The time I'm testing with: 12:05 AM America/Chicago我测试的时间:12:05 AM America/Chicago

I'm attempting to parse as follows.我试图解析如下。

const date = DateTime.fromFormat(value, 'hh:mm a z');

The output is output 是

Hour: 1 (incorrect)小时:1(不正确)

Minute: 05 (correct)分钟:05(正确)

Meridien: (AM) (correct)子午线:(上午)(正确)

Timezone: America/New_York (incorrect)时区:America/New_York(不正确)

If you set opts.setZone to true, it will keep the DateTime in Chicago time, so any output will be displayed in that timezone.如果将opts.setZone设置为 true,它将保留芝加哥时间的 DateTime,因此任何 output 都将显示在该时区。

To display the time in your local zone you can use DateTime.toLocal() .要显示本地区域的时间,您可以使用DateTime.toLocal()

You can switch zone again by using the DateTime.setZone() function if you need to.如果需要,您可以使用DateTime.setZone() function 再次切换区域。

 const { DateTime } = luxon; const value = '12:05 AM America/Chicago'; const date = DateTime.fromFormat(value, 'hh:mm a z', { setZone: true }); console.log('Timezone:', date.toFormat('z')) console.log('Time (Chicago):', date.toFormat('hh:mm a')) console.log('Time (local):', date.toLocal().toFormat('hh:mm a')) console.log('Time (New York):', date.setZone('America/New_York').toFormat('hh:mm a'))
 .as-console-wrapper { max-height: 100%;important; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/2.3.1/luxon.min.js" integrity="sha512-Nw0Abk+Ywwk5FzYTxtB70/xJRiCI0S2ORbXI3VBlFpKJ44LM6cW2WxIIolyKEOxOuMI90GIfXdlZRJepu7cczA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

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

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