Skip to content

Getting previous day instead of current for MSSQL DATEONLY #10231

Closed

Description

@spearmootz

What are you doing?

storing and retrieving a dateOnly field from mssql using tedious

 @IsDate
  @Column({
    type: DataType.DATEONLY,
    get(): moment.Moment | null {
      const date = this.getDataValue('date_of_birth');
      return moment(date);
    },
    set(date: moment.Moment | null) {
      return this.setDataValue(
        'date_of_birth',
        date == null ? null : date.format()
      );
    }
  })
  date_of_birth?: moment.Moment;

What do you expect to happen?

get date in database

What is actually happening?

getting the previous day

Output, either JSON or SQL
SQL returns '1992-10-10'
Actual result is '1992-10-09'

Dialect: mssql
Dialect version: 4.1.0
Database version: XXX
Sequelize version: 5.0.0-beta.14
Tested with latest release: No (If yes, specify that version)

I did my research and i found that in data-types.js in the dialect was doing the following

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

based on this code i can infer that its expecting value to be either an iso format or a date object. it is indeed getting a moment object but with a parsed time in GM (Sat May 05 2018 20:00:00 GMT-0400) so when formatting to 'YYYY-MM-DD' it gets the previous day.

i changed it to
``return moment.utc(value).format('YYYY-MM-DD');`

and it started working. i am not making this a PR because i dont think this is the right approach based on what ive seen and that im not a sequelize expect.

Activity

  1. stale commented on Mar 5, 2019

    @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 🙂

  2. dengue8830 commented on Oct 22, 2019

    @dengue8830

    same problem here, i fixed adding 1 day to the obtained date 😢

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