Skip to content

DATEONLY in MSSQL converted to localtime #10982

Closed

Description

@tonyguinta

What are you doing?

Using DATEONLY, when querying the database directly the date is '2019-06-01', but Sequelize returns '2019-05-31'. I'm in the US Central timezone, so I assume it is still treating it as a UTC date and converting it to my timezone.

It is only returning the string in 'YYYY-MM-DD' format, which is good, but it is converting it based on my local time, which is bad.

Sequelize: 5.8.5
Tedious: 6.1.1
MS SQL Server 2017

What do you expect to happen?

Return the date string as stored without converting based on local timezone.

What is actually happening?

The date string is converted to local time, which displays as the previous day. '2019-06-01' is stored in the database, but '2019-05-31' is returned by Sequelize.

I rolled back to Sequelize 4.41.2 and Tedious 3.0.1 and the same date is returned as '2019-06-01T05:00:00.000Z'. So, it seems that DATEONLY has since been fixed to only return a date string without time and timezone, but it now assumes that the stored date is in UTC and incorrectly converts it to local time.

Summary:

SQL Server DATEONLY Field Value: '2019-06-01'
Sequelize 5.8.5 returns: '2019-05-31'
Sequelize 4.41.2 returns: '2019-06-01T05:00:00.000Z'
My timezone: US Central Time (currently -5:00 with Daylight Saving Time)

Environment

Dialect:

  • mysql
  • postgres
  • sqlite
  • mssql
  • any
    Dialect library version: Tedious 6.1.1
    Database version: MS SQL Server 2017
    Sequelize version: 5.8.5
    Node Version: 8.11.0
    OS: Windows 10
    If TypeScript related: TypeScript version: XXX
    Tested with latest release:
  • No
  • Yes, specify that version: 5.8.6

Activity

  1. added
    dialect: mssql
    status: understood
    type: bug
    on Jul 24, 2019
  2. c1oud45 commented on Aug 4, 2019

    @c1oud45

    Hi, did you find any workaround for this issue?

  3. tonyguinta commented on Aug 21, 2019

    @tonyguinta
    Author

    Hi, sorry for the delay in responding.

    My workaround was to roll back to Sequelize 4.41.2 and Tedious 3.0.1, which still returns a full datetime string for DATEONLY data types, but at least it returns it in the correct timezone.

  4. dead commented on Sep 6, 2019

    @dead

    return moment(value).format('YYYY-MM-DD');

    Changing to:

    return moment.utc(value).format('YYYY-MM-DD');

    Fix this issue for me.

    @papb Can you take a look in this?
    I saw that others dialects uses moment(value) from the BaseType.DATEONLY but I saw nobody reporting problems with DATEONLY

  5. papb commented on Sep 6, 2019

    @papb
    Member

    @dead Hmm... While you're at it, can you test two other things as well?

    Test 1 - Removing the extended DATEONLY class entirely (also removing here)

    Test 2 - Changing parse to an string-casting function:

    static parse(value) {
      return '' + value;
    }

    Thanks!

  6. dead commented on Sep 9, 2019

    @dead

    @papb
    Test 1 - It worked. Didn't work
    Test 2 - Didn't work, the parsed date was in invalid format.

  7. papb commented on Sep 9, 2019

    @papb
    Member

    @dead Nice! Can you submit a PR with my first suggestion?

  8. added
    topic: dates & times
    on Oct 30, 2019
  9. nmeeda commented on Jun 23, 2020

    @nmeeda

    I hope is it still Open?

  10. tonyguinta commented on Oct 5, 2020

    @tonyguinta
    Author

    This is still a problem with Sequelize 6.3.5 and Tedious 9.2.1 on Node 11.3.

    Server Timezone: 'UTC -5:00'
    Date stored in SQL Server: '2020-10-01'
    Value returned from Sequelize 6.3.5 and Tedious 9.2.1 via Postman: '2020-09-30'
    Value returned from Sequelize 4.41.2 and Tedious 3.0.1 via Postman: '2020-10-01T05:00:00.000Z'

    If I change my timezone to UTC and start up Node, then Sequelize 6.3.5 and Tedious 9.2.1 return '2020-10-01'. It's pretty clear that the stored date is treated as UTC and is converting it to the Node server's timezone.

    I have confirmed that the change in this pull request fixes the issue, but has yet to be included in a release.

  11. tonyguinta commented on Oct 6, 2020

    @tonyguinta
    Author

    Pull request 11409 does indeed fix this issue, but it has yet to be included in a Sequelize release.

    As a workaround, I've made the change directly to my installation in node_modules and used patch-package, which allows me to include the change in my source code and deploy it to my higher environments.

  12. azterix commented on Oct 22, 2020

    @azterix

    If you're looking for a workaround and don't want to get in to patching the package while we await that PR making it to release, I discovered this workaround that works for me using the moment-timezone package:

    import moment from 'moment-timezone';
    moment.tz.setDefault('UTC');
    

    Thanks to @dengue8830 for the workaround over on #11597

  13. spearmootz commented on Nov 12, 2020

    @spearmootz
    Contributor

    instead of changing the dependency you can just do this

    class DATEONLY extends BaseTypes.DATEONLY {
    static parse(value) {
    return moment.utc(value).format('YYYY-MM-DD');
    }
    }

    so add .utc(value)

  14. nandanv2702 commented on Sep 13, 2021

    @nandanv2702

    Hey, I recently came across this issue too - using Sequelize version 6.6.5 with the workaround suggested in Pull Request 11409

  15. github-actions commented on Nov 10, 2021

    @github-actions
    Contributor

    This issue has been automatically marked as stale because it has been open for 14 days without activity. It will be closed if no further activity occurs within the next 14 days. If this is still an issue, just leave a comment or remove the "stale" label. 🙂

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

    dialect: mssqlstatus: understoodtopic: dates & timestype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions