I recently had to troubleshoot an issue with our NSW Bus and Train Timetable board tool whereby it was displaying 0 hours instead of 12 for PM. The code was written in Javascript and after some testing, I worked out the problem.
This is how it looked:
Here was the original code:
this.time = Intl.DateTimeFormat(navigator.language, {
hour: 'numeric' ,
minute: 'numeric',
second: 'numeric',
timeZone: 'Australia/Sydney',
hour12: true
}).format()
Although it was displayed in the 12 hours format, it shouldn’t be using 0 hours. I ended up looking for the DateTimeFormat and found out there was a property called hourCycle. Using hourCycle h12, “Hour system using 1–12; corresponds to ‘h’ in patterns. The 12 hour clock, with midnight starting at 12:00 am.”. This is different from h11 where the hour system uses 0-11. When I found this, I changed the hour12: true to hourCycle: ‘h12’ and it fixed the problem.
this.time = Intl.DateTimeFormat(navigator.language, {
hour: 'numeric' ,
minute: 'numeric',
second: 'numeric',
timeZone: 'Australia/Sydney',
hourCycle: 'h12'
}).format()
The result:
There may be some other ways to resolve this issue and I look forward to receiving feedback or another solution in the comments.
If this article helped you in any way and you want to show your appreciation, I am more than happy to receive donations through PayPal. This will help me maintain and improve this website so I can help more people out there. Thank you for your help.
HELP OTHERS AND SHARE THIS ARTICLE
LEAVE A COMMENT
I am an entrepreneur based in Sydney Australia. I was born in Vietnam, grew up in Italy and currently residing in Australia. I started my first business venture Advertise Me from a random idea and have never looked back since. My passion is in the digital space, affiliate marketing, fitness and I launched several digital products. You will find these on the portfolio page.
I’ve decided to change from a Vegetarian to a Vegan diet and started a website called Veggie Meals.
I started this blog so I could leave a digital footprint of my random thoughts, ideas and life in general.
If any of the articles helped you in any way, please donate. Thank you for your help.
Affiliate Compensated: there are some articles with links to products or services that I may receive a commission.