Date and Node.js
20 janvier 2012 Laisser un commentaire
Recently, I did some small developments on Ponyo that made me deal with dates. Unfornately I found a big bug in Node.js about them. By default when you create a date, it changes it and set it one hour before. When you try to modify it, it doesn’t change at all. So I decided to include node-time to deal with timezones and moment to have more human functions to manage dates. But the problem still occurs. There is no good reason for that, when you force a date, it should not be updated to current timezone.
> new Date(2012, 01, 03, 0, 0, 0, 0) Thu, 02 Feb 2012 23:00:00 GMT
I finally find one solution, that I share here, hoping it will save time to people in the same case as me. To avoid this, the timezone must not be set on the date you work on but on the node process. In your “main” just add this at the beginning :
process.env.TZ = 'UTC'
Now you can use date like in any other programming environment.
Edit : Apparently it’s not a Node bug, it’s the way javascript works…