2

I am struggling in formatting js date in a proper format and not sure if I'm doing this right. So here's my case:

I import data strongly dependent on hours included in objects. Sometimes I received date with GMT +1 format and sometimes with GMT +2. I have figured out it depends on daylight saving date format.

But the problem is now, when I try to work on this data and I look for object from december, function getDate() would return me the day before (as hours would be 23:00 GMT +1 NOT 00:00 GMT +2)

I am working on client's oracle database and use it in nodejs server supported by oracledb npm package.

I was wondering if there is any nice / smooth way of unifying these date format to the same one? Like to receive all dates only in one of GMT +1/+2 format?

Thanks for any advice

MT0
  • 143,790
  • 11
  • 59
  • 117
jhrwekuh
  • 186
  • 2
  • 15
  • Timestamps should ideally always be stored in UTC and returned as such to the client, as a result the client can display the date according to the local timezone. Why are the dates not in UTC? https://stackoverflow.com/questions/11537106/is-it-always-a-good-idea-to-store-time-in-utc-or-is-this-the-case-where-storing – lux Jun 05 '20 at 15:14
  • @lux, I am receiving a JS Date object, from the oracledb npm package. I store UTC strings in the database, however, need to operate on date object in order to retrieve date, hours and minutes of the particular date – jhrwekuh Jun 05 '20 at 15:20
  • Are you working with dates on multiple machines? How do some have GMT +1 and others GMT +2? What data type are the dates stored in Oracle Database? If DATE, what is the timezone of the dates stored in the table? – Dan McGhan Jun 05 '20 at 17:24
  • @DanMcGhan, I fetch data from different machine indeed. The type is Date, but timezone difference comes from daylight saving time change. That's why it's sometimes gmt +1 and sometimes +2. – jhrwekuh Jun 05 '20 at 17:57
  • Use *getUTC\** methods, then all machines will get the same values as the local offset will be ignored. – RobG Jun 05 '20 at 20:21
  • The driver will get it's default time zone from the operating system. If you have two different apps working with different timezones, then you may have some problems. The easiest thing to do is have all of your machines working in UTC and if you're using DATE or TIMESTAMP columns in the database, then they should be "implicitly" UTC. You can read this for the long answer: https://jsao.io/2016/09/working-with-dates-using-the-nodejs-driver/ – Dan McGhan Jun 05 '20 at 21:13
  • In summary, [set ORA_SDTZ](https://oracle.github.io/node-oracledb/doc/api.html#datehandling) before you start your Node.js process. – Christopher Jones Jun 05 '20 at 22:25

1 Answers1

3

What you'd like to do here is adjust the time according to getTimezoneOffset() and then store it.

MDN

The time-zone offset is the difference, in minutes, from local time to UTC.

var d = new Date();
console.log(d)//Time before adjusting the timezone
d.setMinutes(d.getTimezoneOffset());
console.log(d) //time adjusted to UTC
ABGR
  • 4,631
  • 4
  • 27
  • 49