Date and Node.js

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…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: