Skip to content

Conversion failed when converting date and/or time from character string. #7930

Closed

Description

@epozsh

on sequelize version 4.3.0, i get this error
cause when i give new Date() sequelize pass this format

2014-04-11 14:38:44.000 +00:00

on sequelize version 3.27.0 i do not get error
cause it pass it like this

2014-04-11 14:38:44.000

How i remove +00:00? so i can use newer version?

Activity

  1. cladjules commented on Aug 14, 2017

    @cladjules

    @Shadowman4205 that may look ugly but, in sequelize/lib/data-types.js, you can replace the method:

    DATE.prototype._stringify = function _stringify(date, options) {
      date = this._applyTimezone(date, options);
    
      // Z here means current timezone, _not_ UTC
      // return date.format('YYYY-MM-DD HH:mm:ss.SSS Z');
      return date.format('YYYY-MM-DD HH:mm:ss.SSS');
    };
    
  2. stale commented on Sep 22, 2017

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂

  3. epozsh commented on Sep 25, 2017

    @epozsh
    Author

    @cladjules Thank you for this solution for now.

  4. carlosscheffer commented on Nov 3, 2017

    @carlosscheffer

    I'm facing the same problem, however it's within the ambit of comparison:
    WHERE [PedidoVenda].[DATA_ALTERACAO] > N'2017-11-01 11:14:03.267 +00:00'

    thats returns me

    DatabaseError: Conversion failed when converting date and/or time from character string.

  5. jnreynoso commented on Jan 29, 2018

    @jnreynoso

    +1

  6. andersao commented on Feb 7, 2018

    @andersao

    +1

  7. balazsnemeth commented on Feb 8, 2018

    @balazsnemeth

    +1 -> Same issue with dateformat "2018-02-08T05:00:00.000Z" on version 4.32.6

  8. added
    type: bug
    on Feb 9, 2018
  9. spyter commented on Feb 12, 2018

    @spyter

    +1

    The date value below is a result of setting processed = new Date();

    UPDATE [dbo].[Alert] SET [Processed]=N'2018-02-12 20:31:19.470 +00:00' OUTPUT INSERTED.* WHERE [Id] = N'27961F66-E5A5-47FE-8B89-0DA140D2C1EC'

  10. RicardoDMelo commented on Feb 18, 2018

    @RicardoDMelo

    +1

  11. Vovka19 commented on Feb 18, 2018

    @Vovka19
  12. balazsnemeth commented on Feb 27, 2018

    @balazsnemeth

    Any news on this?

  13. henryjw commented on Mar 23, 2018

    @henryjw

    The workaround I'm currently using is to use strings instead of dates for my models and then passing the ISO string instead of the date object.

    // Doesn't work because new Date() is an object, not a string
    MyModel.create({
       imported_date: new Date()
    });
    
    // Works (if type of imported_date is STRING)
    MyModel.create({
       imported_date: new Date().toISOString()
    });
    

    For an actual solution, maybe another data type can be added to avoid adding the timezone? DATE_NO_TIMEZONE?

  14. 7 remaining items

  15. v1d3rm3 commented on Dec 21, 2018

    @v1d3rm3

    No news?

  16. juniortarcisio commented on Dec 21, 2018

    @juniortarcisio

    What do you all think about sending a pull request with some fix for this situation? I don't know how the devs are working properly to treat this mismatch logics between different RDMSs, so I don't feel confident enough to pull request it.

  17. molaux commented on Apr 17, 2019

    @molaux

    Had the same problem with mssql and found that casting to DATETIMEOFFSET worked for me in my where clause, without removing timezone serialization in DATE _stringify prototype :

    Sequelize.cast(dateObj, 'DATETIMEOFFSET')
    

    Don't know how to get Sequelize handle this directly...

  18. added
    topic: dates & times
    on Oct 30, 2019
  19. epozsh commented on May 4, 2020

    @epozsh
    Author

    So this is issue still on!
    Any news for regarding this?

  20. randomaxess commented on Feb 24, 2022

    @randomaxess

    This is still an issue.

  21. Naim-Bijapure commented on Mar 23, 2022

    @Naim-Bijapure

    still an issue

  22. ephys commented on Mar 23, 2022

    @ephys
    Member

    This issue has been open for a long time and I believe it's because we don't have enough information to properly understand it.

    According to the documentation, DataTypes.DATE is supposed to be DATE WITH TIMEZONE (or equivalent).

    To resolve this issue we need to know:

    • Which dialects are impacted
    • Which Sequelize version is in use

    Providing a SSCCE that reproduces this issue would also greatly help getting this resolved.

  23. byron70 commented on Apr 22, 2022

    @byron70

    This is more to note for future reference, then request a fix.

    My scenario is with mssql dialect. Sequelize: [email protected]. MS SQL 2014 and 2017.

    The database was not generated by Sequelize and I cannot change the data type of the column from datetime to datetimeoffset.

    In the example Sequelize is generating the following string for a date. Which cannot be explicitly converted by mssql to datetime.

    select * from stock 
    -- stockTSCreate is datetime
    WHERE stockTSCreated > N'2022-04-20 16:09:54.000 +00:00';
    
    Conversion failed when converting date and/or time from character string.
    

    The workaround to Sequelize.cast(lastUpdated, 'DATETIMEOFFSET') works just fine.

  24. ephys commented on Apr 22, 2022

    @ephys
    Member

    @byron70 Then I have some good news. Adding support for datetime2 (without offset) for mssql is somewhere on my backlog #14295

  25. qq578023708 commented on Jun 18, 2022

    @qq578023708

    Up to now, this problem has existed for at least 3 years, and the current version 6.21 has not been repaired. If your database has a date type primary key, if you use sequelize Save() method,although you modify the _stringify () method, but it is not affected. It still throws you type conversion errors. Perhaps the best way is to directly change your model Sequelize.DATE to Sequelize.STRING()

    dialect: mssql
    sequelize version: 6.21
    database data value: 2022-04-20 16:09:54.000

  26. Lancer92 commented on Jan 20, 2023

    @Lancer92

    Still not works on Windows with mssql 2019
    sequalize version: 6.28.0
    column type: datetime
    DATA_TYPE: DATE

  27. ephys commented on Jan 20, 2023

    @ephys
    Member

    As indicated above, DataTypes.DATE is mapped to datetimeoffset, not datetime nor datetime2. If you need a DataType that maps to datetime or datetime2, here are the relevant issues:

    While we do plan on supporting datetime2, we are not currently planning on adding support for mssql's datetime data type. This will need to be handled by writing a custom data types or extending an existing one (Sequelize 7)

    These are the relevant issues, please head to these instead

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

    topic: dates & timestype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions