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

2019-04-28 05:36发布

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) ?

5条回答
兄弟一词,经得起流年.
2楼-- · 2019-04-28 05:57

The way I handled the problem after hours of research :

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

in config/bootstrap.js

查看更多
Lonely孤独者°
3楼-- · 2019-04-28 05:58

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!

查看更多
孤傲高冷的网名
4楼-- · 2019-04-28 06:07

The best architecture planning here, IMO, is to continue using Sails.js isoDate formatting. When you're user's load your website/app the isoDate will be converted to their client/browser timezone which is usually set at the OS level.

Here's an example you can test this out with. Open a browser console and run new Date().toISOString() and look at the time it sets. It's going to be based of off the spec for isoDate 8601 (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString). Now, change your system time to a different time zone or simply change your hour on the time and save (you shouldn't have to reload if you're using chrome console). Run your command in the console again new Date().toISOString() and you'll get an adjusted time appropriate to the time you just changed.

If you'd like to continue on proving to yourself the time Sails.js is appropriate to use, use Moment.js on an isoDate that is stored in your database (created by waterline ORM) like so moment("2016-02-05T22:36:48.800Z").fromNow() and you'll notice the time is relative to your system time.

查看更多
贪生不怕死
5楼-- · 2019-04-28 06:11

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'
  },
查看更多
冷血范
6楼-- · 2019-04-28 06:17

I've come to grips with not setting a timezone at the app level (I see why the sails authors did it that way), however I've been having a rough time performing a simple date match query. I'd assume that if you create a record using the default blueprint methods (this one containing an extra datetime field over the defaults), passing in a date, that you'd be able to pass in the same date in a get query and get the same record.

For example, let's say the datetime field is called "specialdate". If I create a new record through the api with "specialdate" equaling "06-09-2014" (ignoring time), I haven't been able to run a find query in which I can pass in "06-09-2014" and get that record back. Greater than queries work fine (if I do a find for a date greater than that). I'm sure it's a timezone offset thing, but haven't been able to come up with a solution.

查看更多
登录 后发表回答