I know there’s lots and lots and lots of threads discussing the various head-banging difficulties involved in manipulating date/time objects.
This is not an attempt to solve every single-dingle issue involved with dealing with date/times, but a lot of these discussions are about calendars and displaying dates and just wanting to be able to treat a DATE (such as "August 10, 2018) as the SAME date but in a different timezone.
"I’m trying to display a date on [insert your fave calendar solution here], but it’s not working right because the date in question was created with timezone X, but the person viewing my calendar is in timezone Y.
This would kind of be easy if I could just turn the date part in timezone X into the same date part in timezone Y, essentially throwing away the time part, but ‘transmuting’ the date into the same date in timezone Y. Why is it so hard to do that?"
myDate is July 29, 2018 at 00:00:00 in Pacific Daylight Time (that is, it indicates the start of that particular day):
console.log(myDate); // "Sun Jul 29 2018 00:00:00 GMT-0700 (Pacific Daylight Time)"
If this date is displayed in a calendar where the viewer is ALSO in PDT, they will see the date as blocked (or whatever) just fine. However, if the viewer is SOMEWHERE else, this date may show up funky, right? (Or not show up at all. Totally depends on how your calendar figures this out.)
Let’s say you want to take this date which is currently in the “America/Los Angeles” timezone ID / Pacific Daylight Time timezone and construct the same date, but in the “America/New York” timezone ID / Eastern Daylight Time timezone.
Here’s the one line of code that will do just that:
myDateInAnotherZone = moment.tz(String(moment.utc(myDate).format('YYYY-MM-DD')), 'YYYY-MM-DD', zone).toDate();
In this case zone would be:
console.log(zone); // "America/New York"
console.log(myDateInAnotherZone); // "Sat Jul 28 2018 21:00:00 GMT-0700 (Pacific Daylight Time)"
(Do it in your head: 21:00 hours in Pacific on Saturday night is in fact the first moment of Sunday morning in Eastern.)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js "></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.21/moment-timezone-with-data.js"></script>
Once those scripts are loaded, you can access moment-timezone like I did in the single line of code above.
How does this work? There’s kind of a lot going on in that one line of code, yeah?
Well, the general technique here is this:
Now, you have the date as a string that is set free from the time element, timezones and all of that (important but annoying) nonsense.
We take that string and turn it into a moment (which is like a date but fancier). Anyway, using moment-timezone, we can construct the moment and ASSIGN IT THE TIMEZONE indicated by zone. (So now we have it’s like: Sun Jul 29 2018 00:00:00 but in Eastern Time.)
So, some variations: Let’s say you have a string date already (not a date object) like myStringDate is “2018-07-29”. The expression becomes:
myDateInAnotherZone = moment.tz(myStringDate, 'YYYY-MM-DD', zone).toDate();
The ‘YYYY-MM-DD’ part can be changed to other things of course. Like, if your date is like “07/29/2018” you could change that to ‘MM/DD/YYYY’. Moment is pretty flexible like that.
Anyway, I just wanted to leave this here in the hopes it’ll help someone.