1

I am running an node js - express api using moment.js. My current TZ-Location is Europe/Berlin.(local) I had a inkonsistent state between my develop environment(local) and my production environment. While using the moment library in develop/local environment ive got an utc offset of 120min. But the Google Cloud standard AppEngine doesnt have an offset. So I get a date, which is 2h lower than needed. I was seaching for a while but dont found the right answer to set the local timezone or a local offset for the appengine to work with.

Does someone know, how to fix this problem globaly without changing all date-parsing statements? --> moment()

Paul Werner
  • 87
  • 1
  • 8

2 Answers2

0

If you want to set the timezone globally for moment you can do that by

const moment = require('moment-timezone')

moment.tz.setDefault('Europe/Berlin')

https://momentjscom.readthedocs.io/en/latest/moment/03-manipulating/09-utc-offset/

razki
  • 1,171
  • 2
  • 8
  • 16
  • i've tried that with the moment-timezone plugin, but it doesnt worked – Paul Werner Apr 11 '20 at 15:05
  • Interesting. Well I'd advise storing timestamps in unix time and converting to your timezone. – razki Apr 11 '20 at 15:13
  • do i have to update all required statements in the js files? – Paul Werner Apr 11 '20 at 15:14
  • I've added to app.js: var moment = require('moment-timezone'); moment.tz.setDefault('Europe/Berlin'); // Now I see the required offset console.log(moment()) console.log(moment().format("YYYY-MM-DD HH:mm:ss")) console.log(moment().utcOffset()) console.log(new Date()) //While parsing sth from a DB Thu Apr 09 2020 09:24:58 GMT+0200 (GMT+02:00) It will still output 2h to much in the moment(Thu Apr 09 2020 09:24:58 GMT+0200 (GMT+02:00)).format('YYYY-MM-DD HH:mm:ss') – Paul Werner Apr 11 '20 at 15:25
0

You can use global instance and change tz using moment.tz

More: https://momentjscom.readthedocs.io/en/latest/moment-timezone/01-using-timezones/05-default-timezone/

moment.tz.setDefault("Europe/Berlin");

Also can see list of TZ:

How to get list of all timezones in javascript

xdeepakv
  • 7,835
  • 2
  • 22
  • 32