Skip to content

sequelize changes the datetime value when fetching it from DB #2590

Closed

Description

@tomwang1013

Mysql(timezone is Beijing +08000):

mysql> select begin_time from deals where id = 677053;
+---------------------+
| begin_time          |
+---------------------+
| 2014-11-19 16:25:00 |
+---------------------+

Rails(which proves correctly in our projects):

irb(main):034:0> d.begin_time
=> 2014-11-19 16:25:00 +0800
irb(main):031:0> d.begin_time.to_i
=> 1416385500
irb(main):032:0> Time.at(1416414300)
=> 2014-11-20 00:25:00 +0800

Sequelize:

> app.models.Deal.find({ where: { id: 677053 } }).ok(function(d) { console.log(d.begin_time.getTime()); });

1416414300000

Obviously, sequelize treat the time in mysql as UTC which is realy +08000. So the difference between mysql and sequelize is 8 hours.

Activity

  1. janmeier commented on Nov 19, 2014

    @janmeier
    Member

    You can fix this by telling sequelize what time zone your db is in

    new Sequelize(... , {
      timezone: '+08:00'
    })
  2. janmeier commented on Nov 19, 2014

    @janmeier
    Member

    And please avoid posting the same problem in several issues :)

  3. tomwang1013 commented on Nov 19, 2014

    @tomwang1013
    Author

    @janmeier Setting the timezone didn't work, the fetched time is still the same:

    > app.models.Deal.find({ where: { id: 677053 } }).ok(function(d) { console.log(d.begin_time.getTime()); });
    
    1416414300000
    > sequelize.options.timezone
    '+08:00'
  4. janmeier commented on Nov 19, 2014

    @janmeier
    Member

    Which version are you on?

  5. tomwang1013 commented on Nov 19, 2014

    @tomwang1013
    Author

    1.7.8

  6. janmeier commented on Nov 19, 2014

    @janmeier
    Member

    Sorry, that only works on 2.0. There is no planned fix for 1.7, so I'd suggest you to upgrade

  7. tomwang1013 commented on Nov 19, 2014

    @tomwang1013
    Author

    @janmeier Is 2.0 compatible with 1.7? i.e. the code of 1.7 still works in 2.0?

  8. janmeier commented on Nov 19, 2014

    @janmeier
    Member

    Mostly, but there are some breaking changes, hence the major version increment. Look at https://github.com/sequelize/sequelize/wiki/Upgrading-to-2.0 and the changelog to see what changed

  9. janmeier commented on Dec 3, 2014

    @janmeier
    Member

    Closing, please reopen if the issue persists in 2.0 @tomwang1013

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions