Previous answers on here pointed me to moment.js for javascript date handling and I am really happy to find it. I can parse and manipulate quite happily.
The users on my website look at info relating to diverse physical sites/locations, and I want to show associated datetimes in the time specific to that location, not the users location.
Each physical site has a timezone attribute string like "Europe/London" or "Europe/Amsterdam"
My datetimes are all stored and delivered from the DB in UTC.
Is there a clever simple way I can render my moment.js object in any specified timezone?
Theoretically, you could do something like this.
moment.fn.formatInZone = function(format, offset) {
return this.clone().utc().add('hours', offset).format(format);
}
moment().formatInZone('HH:mm:ss', 7);
moment().formatInZone('HH:mm:ss', -7);
However, this requires that you know the correct offset, so it won't take into consideration daylight saving time.
If you want to display dates in ANOTHER timezone than the user is actually in, then you need to start looking into stuff like https://bitbucket.org/pellepim/jstimezonedetect (if you need to detect which timezone a user is in) and https://github.com/mde/timezone-js if you need to localize dates between zones.
jsTimezoneDetect which I linked to above will help you provide a shortlist of relevant timezones.
That you have your datetimes stored in UTC will make it pretty damn easy for you thanks to mde/timezone-js.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With