'How to use Moment.JS to convert to 12 hour clock or 24 hour clock based on timezone
So I'm starting with a REALLY weird format, which is essentially the minutes after midnight represented as a number (in the timezone I'm trying to get to). So if I'm in New York and mean 6:30 AM, the starting value is 390.
Here's my code so far:
const open_time = (String(startingTime / 60)).split(".") // startingTime of 390 becomes an array of ['6','5']
const open_hour = open_time[0] // open hour becomes '6'
var open_minutes = 0 // if the startingTime was evenly divisible by 60, that means we're on the hour
if (open_time[1] !== undefined) { // startingTime wasn't evenly divisible by 60, need to do some calculations
open_minutes = Number(`0.${open_time[1]}`) * 60 // get the minutes into the hour
}
const open_minutes_formatted = open_minutes.toLocaleString('en-US', {
minimumIntegerDigits: 2,
useGrouping: false
}) // make sure the minutes format in 00 - 09 instead of 0 - 9 for the first 10 minutes of the hour.
const hours = `${open_hour}:${open_minutes_formatted}` // final string outputted
At this point, hours is formatted to something like 6:30, or if it's in the afternoon, 15:00. My question is, how do I use moment.js to format this time based on the 12 hour clock vs 24 hour clock based on where the user is?
At this point in the code, I've used the module tzlookup to get the timezone for where the user is located so I think I can use that, but not sure how the actual conversion looks. Any help appreciated!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
