Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dayjs("2021-01-26T14:25:04.871Z", "YYYY-MM-DDTHH:mm:ss.SSSZ") doesn't work #1358

Closed
beckyconning opened this issue Jan 26, 2021 · 5 comments
Closed

Comments

@beckyconning
Copy link
Contributor

beckyconning commented Jan 26, 2021

Describe the bug
dayjs("2021-01-26T14:25:04.871Z", "YYYY-MM-DDTHH:mm:ss.SSSZ") doesn't work
moment("2021-01-26T14:25:04.871Z", "YYYY-MM-DDTHH:mm:ss.SSSZ") works

Expected behavior
Should work as moment does treating Z as +00:00

Information

  • Day.js Version 1.10.3
  • OS: Node v10.16.3
  • Browser: Node v10.16.3
  • Time zone: London
@iamkun
Copy link
Owner

iamkun commented Jan 27, 2021

fixed in #1359

@iamkun iamkun closed this as completed Jan 27, 2021
@ghiscoding
Copy link

ghiscoding commented Feb 5, 2021

@beckyconning
Have you tried this code change in strict mode?

Since this is not released yet so I tried to manually modify the customParseFormat.js minified file with your code change and in strict mode it seems to fail the isValid check.

const format = 'YYYY-MM-DDTHH:mm:ss.SSSZ';
const inputDate = '1993-12-01T10:22:33.128Z';
const isDateValid = dayjs(inputDate, format, true).isValid(); // returns false

I use this Zulu format a lot in my open source lib and this is the only piece missing to migrate to DayJS

so I then cloned the project and tried adding this unit test in customParseFormat.test.js and it indeed fails

describe('Strict mode', () => {
  // ...

  it('zulu', () => {
    const input = '2021-01-26T15:38:43.000Z'
    const format = 'YYYY-MM-DDTHH:mm:ss.SSSZ'
    expect(dayjs(input, format).isValid()).toBe(true)         // ok
    expect(dayjs(input, format, true).isValid()).toBe(true)  // fails
  })
})

I traced the problem up to the date !== this.format(format) and that is where it fails because the date is not equal to the format result, so I think the format() function has to replace the Z by -00:00 or something like that? I'm not sure what to do to fix this

these lines 227-229

// in my case date is '2021-01-26T15:38:43.000Z' 
// and this.format() is '2021-01-26T10:38:43.000-05:00' 
// so they are not strictly equal and it fails

      if (isStrict && date !== this.format(format)) {
        this.$d = new Date('')
      }

@tbastyns
Copy link

tbastyns commented Jan 21, 2022

@ghiscoding

When I escape the 'Z' as such 'YYYY-MM-DDTHH:mm:ss.SSS[Z]', I get the expected results when I use strict mode:

  expect(dayjs('2022-01-21T12:12:12.123Z', 'YYYY-MM-DDTHH:mm:ss.SSS[Z]').isValid()).toBeTruthy();
  expect(dayjs('2022-01-21T12:12:12.123Z', 'YYYY-MM-DDTHH:mm:ss.SSS[Z]', true).isValid()).toBeTruthy();
  // set month to 13; should not be accepted in strict mode
  expect(dayjs('2022-13-21T12:12:12.123Z', 'YYYY-MM-DDTHH:mm:ss.SSS[Z]').isValid()).toBeTruthy();
  expect(dayjs('2022-13-21T12:12:12.123Z', 'YYYY-MM-DDTHH:mm:ss.SSS[Z]', true).isValid()).toBeFalsy();

(don't forget to extend customParseFormat!)

@ghiscoding
Copy link

ghiscoding commented Jan 21, 2022

oh ok I didn't know we had to escape it [Z], thanks for the clarification, will test it later.
BTW, your last 2 lines have the same date input (month 13), I think you meant month 12 for the truthy one

@tbastyns
Copy link

the last 2 lines illustrate one of the differences of strict VS non-strict: only when using strict, a 13th month is considered invalid

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants