Skip to content

Return Date values in UTC Timezone #1035

Closed

Description

@jonhkr

The date values are being saved in UTC format but when retrieved from the DB they are being converted to the local timezone.

Ex:
Saved DATE in the DB: 2013-11-02 00:00:00
Returned date from Model.date: Sat Nov 01 2013 22:00:00 GMT-0200 (BRST)

I tried to find where the date values are parsed but found nothing, if anyone can point me where it is i can try to fix it.

Activity

  1. janmeier commented on Nov 4, 2013

    @janmeier
    Member

    I originally rewrote the date logic to UTC, so I would like to elaborate on why I think this is the correct behavour. Mind you I haven't looked at the code lately, so im just speaking from memory :).

    The two dates you posted represent the same point in time:

    d1 = new Date('2013-11-02 00:00:00Z')
    Sat Nov 02 2013 01:00:00 GMT+0100 (W. Europe Standard Time)
    d2 = new Date('Sat Nov 01 2013 22:00:00 GMT-0200 (BRST)')
    Sat Nov 02 2013 01:00:00 GMT+0100 (W. Europe Standard Time)
    
    d1.getTime()
    1383350400000
    d2.getTime()
    1383350400000
    d1.getTime() === d2.getTime()
    true

    Model.date is a javascript Date object, and those are always in the local timezone. If you want to the get the UTC time of a Date, you will have to write custom logic using .getUTCDate() etc. When you pass a date object to sequelize it is converted to UTC before it is saved, not matter what local time zone it has. When you retrieve a date object is created, representing the UTC timestamp in the local timezone of your server. This means that even if you save a date from a server in one timezone and retrieve it from a server in another zone, the date objects will represent the same point in time (their timestamps are equal), even though their .toString() representation will be different.

    In short you should not take extra care to convert your dates to UTC before passing them to sequelize, sequelize will handle the conversion.

  2. jonhkr commented on Nov 4, 2013

    @jonhkr
    Author

    So, I solved my problem by converting the date value to UTC again using a model getter method.
    Thanks @janmeier for clarifying it.

  3. janmeier commented on Nov 5, 2013

    @janmeier
    Member

    You're welcome :)

  4. tomwang1013 commented on Nov 19, 2014

    @tomwang1013

    @janmeier I think the problem exists.
    d1 = new Date('2013-11-02 00:00:00Z')
    You added Z which change the time making it different from the one in mysql.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions