Format date in a specific timezone

I’m using Moment.js to parse and format dates in my web app. As part of a JSON object, my backend server sends dates as a number of milliseconds from the UTC epoch (Unix offset).

Parsing dates in a specific timezone is easy — just append the RFC 822 timezone identifier to the end of the string before parsing:

// response varies according to your timezone
const m1 = moment('3/11/2012 13:00').utc().format("MM/DD HH:mm")

// problem solved, always "03/11 17:00"
const m2 = moment('3/11/2012 13:00 -0400').utc().format("MM/DD HH:mm")

console.log({ m1, m2 })
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

But how do I format a date in a specifc timezone?

I want consistent results regardless of the browser’s current time, but I don’t want to display dates in UTC.

8 Answers
8

Leave a Comment