DateTime.hour is not in a 24-hour format after setting the time zone
See original GitHub issueDescribe the bug
The documentation states that the hour
property of DateTime is always in a 24-hour format, but that doesn’t appear to be true in React Native using Expo. If you use Settings.default or DateTime.setZone() to change the time zone, the hour
property is returned in a 12-hour format.
To Reproduce console.log(‘5:00 PM EST hour:’, DateTime.fromISO(‘2021-11-05T17:00:00-04:00’).setZone(‘America/New_York’).hour);
Actual vs Expected behavior The code above should log out “5:00 PM EST hour: 17” but instead logs out “5:00 PM EST hour: 5”.
Desktop (please complete the following information):
- OS: iOS and Android
- Browser: this is happening outside of a browser
- Luxon version: 2.0.2
- Your timezone: America/New_York
Additional context Updated my initial post to make it clear that the issue seems to be specific to mobile devices and/or React Native using Expo.
Issue Analytics
- State:
- Created 2 years ago
- Comments:12 (6 by maintainers)
Top Results From Across the Web
Does DateTime.Now always return a 24 hour format by default?
DateTime.Now returns time without formatting. Format applied in the ToString("HH:mm") method. And yes, this format is 24-hour.
Read more >Set 12 vs 24 hour with the new date formatting API in iOS 15
The problem that I'm having is figuring out how to force the new API to display 12 vs 24h times. So, for example,...
Read more >Time Format, How to convert 12 hour time to 24 hour time? How
I am converting as below, gmt(local(now(),"EST")). Time is changing fine, but the timezone is not changing. Still its displaying CST ...
Read more >Change the date and time on your Galaxy phone or tablet
Navigate to and open Settings. Tap General management, and then tap Date and time. · Tap the switch next to Use 24-hour format....
Read more >Ability to Choose Time Formats - AM/PM or 24 Hour
24 hour time can often cause confusion when setting appointments resulting in the wrong time being set. Having a choice on date and...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@icambron I just updated to version 2.1.1 and the hour for 5:00 PM is now showing as 17. Seems like it worked! Thank you so much!
Ok, I’m now pretty confident this is what’s happening. Here’s the full backstory:
formatToParts
to extract the local time. It can then use that time to compute the zone’s offset at that time.hourCycle
, there was another option calledhour12
. You set it if you wanted to override the locale settings and use 24-hour time insteadhourCycle
was introduced with values likeh11
,h23
, andh24
and the spec explained how to maphour12
to these new values. The gist of the conversion was thathour12: true
turned intoh24
h23
is what people mean by “24-hour time”. When browsers updated to implement this behavior, they broke Luxon. Here’s a discussion: https://bugs.chromium.org/p/chromium/issues/detail?id=1025564&can=2&q=“24%3A00” datetimeformathour12
arguments to Intl. This was harder to fix, because not all browsers supportedhourCycle
yet, so Luxon couldn’t use ithourCycle
was now strong enough, and flipped everything to usehourCycle: 'h23'
hourCycle
is ignored and we sometimes interpret PM times as AM times.So here’s the deal:
hourCycle
in the offset calculator, and use the old workaround there. That will fix this bug without changing any other behavior. (There are several ways to fix it, including simply changing the locale used, but this one is simplest)hourCycle
in the formatting presets likeTIME_24_SIMPLE
, which are used intoLocaleString()
and similar methods. That means that for these older environments, the 24-hour part will continue to be ignored for those presets. You can always specify your own using hour12 instead of hourCycle. This is needed because most callers would get incorrect results for midnight.