1

If I want to build a moment object, representing a timestamp in certain time zone, e.g. 2019/04/13 00:00:00 at time zone Europe/Berlin, how do I do it right?

What I'm trying to do:

moment.tz.setDefault('Europe/Berlin');
const m = moment('2019/04/13 00:00:00');

which results in m being set to Sat Apr 13 2019 02:00:00 GMT+0200 (Central European Summer Time) - 2 hours ahead of what I need. What I need is 00:00:00 not 02:00:00.

What is the reasoning behind this behavior? How do I tell moment-timezone "take this date and time and interpret it as if it is in the time zone I say"?

benvc
  • 14,448
  • 4
  • 33
  • 54
tristantzara
  • 5,597
  • 6
  • 26
  • 40
  • 1
    Possible duplicate of [How to create time in a specific time zone with moment.js](https://stackoverflow.com/questions/18448347/how-to-create-time-in-a-specific-time-zone-with-moment-js) – benvc Apr 16 '19 at 15:15
  • @benvc thanks for pointing me to that question, but checking that still didn't clarify why do I get 02:00:00 instead of 00:00:00, applying recommendations from there (e.g. providing format string) didn't fix the issue as well – tristantzara Apr 16 '19 at 15:37

1 Answers1

0

Per your comment, following is a snippet that demonstrates how to apply the answer from the duplicate question.

const berlin = moment.tz('2019/04/13 00:00:00', 'YYYY/MM/DD hh:mm:ss', 'Europe/Berlin');

console.log('UTC', berlin.utc().format());
// UTC 2019-04-12T22:00:00Z

console.log('Europe/Berlin', berlin.tz('Europe/Berlin').format());
// Europe/Berlin 2019-04-13T00:00:00+02:00
<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.23/moment-timezone-with-data.js"></script>
benvc
  • 14,448
  • 4
  • 33
  • 54