Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set local timezone in Sails.js or Express.js

When I create or update record on sails it write this at updateAt:

updatedAt: 2014-07-06T15:00:00.000Z

but I'm in GMT+2 hours (in this season) and update are performed at 16:00.

I have the same problem with all datetime fields declared in my models.

How can I set the right timezone on Sails (or eventually Express) ?

like image 752
AlexB Avatar asked Jun 08 '14 09:06

AlexB


People also ask

How do I get current timezone in node JS?

To get the current browser's time zone, you can use the getTimezoneOffset() method from the JavaScript Date object. The getTimezoneOffset() returns the time difference, in minutes, between UTC time and local time.

How do I get current time zone in JavaScript?

The JavaScript getTimezoneOffset() method is used to find the timezone offset. It returns the timezone difference in minutes, between the UTC and the current local time. If the returned value is positive, local timezone is behind the UTC and if it is negative, the local timezone if ahead of UTC.

Does JavaScript default to UTC?

The JavaScript Date is always stored as UTC, and most of the native methods automatically localize the result.


3 Answers

The way I handled the problem after hours of research :

Put process.env.TZ = 'UTC'; //whatever timezone you want

in config/bootstrap.js

like image 173
Edouard Menayde Avatar answered Sep 22 '22 10:09

Edouard Menayde


I solved the problem, you should setting the MySql options file to change timezone to UTC in the config/connections.js setting at this

devMysqlServer: {
    adapter: 'sails-mysql',
    host: '127.0.0.1',
    user: 'root',
    password: '***',
    database: '**',
    timezone: 'utc'
  },
like image 43
nicesu Avatar answered Sep 23 '22 10:09

nicesu


Trying to solve your problem by setting the timezone on your server is a bit short-sighted. What if you move? Or someone in a different country accesses your application? The important thing is that the timestamps in your database have a timezone encoded in them, so that you can translate to the correct time on the front end. That is, if you do:

new Date('2014-07-06T15:00:00.000Z')

in your browser console, you should see it display the correct date and time for wherever you are. Sails automatically encodes this timestamp for you with the default updatedAt and createdAt fields; just make sure you always use a timezone when saving any custom timestamps to the database, and you should be fine!

like image 39
sgress454 Avatar answered Sep 22 '22 10:09

sgress454