0

I have a Date object, let's say:

const d = new Date('2014-12-01T12:00:00Z');

And I have a timezone name, let's say:

const timezoneName = 'America/New_York';

What would be an elegant way to create another Date object (or just getting its UTC timestamp) with the same date, but at 9:00am of timezone (by timezoneName)?

I could convert d to a String using toLocaleString and then manipulate this string and then convert it back to Date, but I don't think it's a very elegant solution.

I don't mind using moment.js

Thank you in advance :)

Alexander
  • 7,484
  • 4
  • 51
  • 65
  • 1
    Does this answer your question? [Convert date to another timezone in JavaScript](https://stackoverflow.com/questions/10087819/convert-date-to-another-timezone-in-javascript) – Ryan Wilson Dec 03 '19 at 16:58
  • @RyanWilson thank you, but no. As I've mentioned in my question, I'm not interested in a String, but Date object or its timezone and I would like to not convert it to string as part of the process – Alexander Dec 03 '19 at 17:07
  • If you read through the answers on that post there are some which utilize `moment` and other options. – Ryan Wilson Dec 03 '19 at 17:10
  • Thanks a lot! But all that answers or manipulate Strings or return a String. But I think I've just found a good solution. I appreciate your help! – Alexander Dec 03 '19 at 17:20

1 Answers1

0

Well, I think this one works for me:

const moment = require('moment-timezone');

const d = new Date('2014-12-01T12:00:00Z');
moment(d).tz(timezoneName).set({hour: 9, minute: 0, second: 0, millisecond: 0}).valueOf()
Alexander
  • 7,484
  • 4
  • 51
  • 65
  • `moment(new Date()).tz(timezoneName)` can just be `moment.tz(timezoneName)` (or `moment.tz(input, timezoneName)` to fit your original code). But also, you should prefer [Luxon](https://moment.github.io/luxon/) over Moment for new projects. Moment is in legacy mode. (And no, you cannot yet do this with just `Date`. Eventually, [`Temporal`](https://github.com/tc39/proposal-temporal) will solve this.) – Matt Johnson-Pint Dec 03 '19 at 21:19