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

Parsing with Unix Timestamp tokens (X/x) and other visual tokens in strict mode results in Invalid Date #1569

Closed
safareli opened this issue Jul 6, 2021 · 6 comments · Fixed by #1571
Labels

Comments

@safareli
Copy link

safareli commented Jul 6, 2021

Describe the bug
When X/x token is used and there are visual/escaped tokens in strict mode you get Invalid Date, best described with this code snippet and log result:

source:
const dayjs = require('dayjs');
dayjs.extend(require('dayjs/plugin/customParseFormat'))
dayjs.extend(require('dayjs/plugin/advancedFormat'))

console.log(
  [
    [ 'now format',
      () => dayjs().format('x'),
      () => dayjs().format('X'),
    ],

    [ 'x',
      () => dayjs('/Date(1623787200000)/', '/[Date](x)/').format('/[Date](x)/'),
      () => dayjs('/1623787200000/', '/X/').format('/[Date](X)/'),
      () => dayjs('--1623787200000--', '--X--').format('/[Date](X)/'),
      () => dayjs('1623787200000', 'x').format('x'),
      () => dayjs('1623787200000', 'x').format('YYYY-MM-DD'),
    ],

    [ 'X',
      () => dayjs('/Date(1623787200)/', '/[Date](X)/').format('/[Date](X)/'),
      () => dayjs('/1623787200/', '/X/').format('/[Date](X)/'),
      () => dayjs('--1623787200--', '--X--').format('/[Date](X)/'),
      () => dayjs('1623787200', 'X').format('X'),
      () => dayjs('1623787200', 'X').format('YYYY-MM-DD'),
    ],

    [ 'x STRICT',
      () => dayjs('/Date(1623787200000)/', '/[Date](x)/', true).format('/[Date](x)/'),
      () => dayjs('/1623787200000/', '/x/', true).format('/[Date](x)/'),
      () => dayjs('--1623787200000--', '--x--', true).format('/[Date](x)/'),
      () => dayjs('1623787200000', 'x', true).format('x'),
      () => dayjs('1623787200000', 'x', true).format('YYYY-MM-DD'),
    ],

    [ 'X STRICT',
      () => dayjs('/Date(1623787200)/', '/[Date](X)/', true).format('/[Date](X)/'),
      () => dayjs('/1623787200/', '/X/', true).format('/[Date](X)/'),
      () => dayjs('--1623787200--', '--X--', true).format('/[Date](X)/'),
      () => dayjs('1623787200', 'X', true).format('X'),
      () => dayjs('1623787200', 'X', true).format('YYYY-MM-DD'),
    ],
  ]
    .map(
      ([name, ...cases]) =>
        '\n// ' +
        name +
        '\n' +
        cases.map((f) => `${f.toString().substring(6)} // ${f()}`).join('\n')
    )
    .join('\n')
);

output:

// now format
dayjs().format('x') // 1625563406782
dayjs().format('X') // 1625563406

// x
dayjs('/Date(1623787200000)/', '/[Date](x)/').format('/[Date](x)/') // /Date(1625515200000)/
dayjs('/1623787200000/', '/X/').format('/[Date](X)/') // /Date(1625515200)/
dayjs('--1623787200000--', '--X--').format('/[Date](X)/') // /Date(1625515200)/
dayjs('1623787200000', 'x').format('x') // 1623787200000
dayjs('1623787200000', 'x').format('YYYY-MM-DD') // 2021-06-16

// X
dayjs('/Date(1623787200)/', '/[Date](X)/').format('/[Date](X)/') // /Date(1625515200)/
dayjs('/1623787200/', '/X/').format('/[Date](X)/') // /Date(1625515200)/
dayjs('--1623787200--', '--X--').format('/[Date](X)/') // /Date(1625515200)/
dayjs('1623787200', 'X').format('X') // 1623787200
dayjs('1623787200', 'X').format('YYYY-MM-DD') // 2021-06-16

// x STRICT
dayjs('/Date(1623787200000)/', '/[Date](x)/', true).format('/[Date](x)/') // Invalid Date
dayjs('/1623787200000/', '/x/', true).format('/[Date](x)/') // Invalid Date
dayjs('--1623787200000--', '--x--', true).format('/[Date](x)/') // Invalid Date
dayjs('1623787200000', 'x', true).format('x') // 1623787200000
dayjs('1623787200000', 'x', true).format('YYYY-MM-DD') // 2021-06-16

// X STRICT
dayjs('/Date(1623787200)/', '/[Date](X)/', true).format('/[Date](X)/') // Invalid Date
dayjs('/1623787200/', '/X/', true).format('/[Date](X)/') // Invalid Date
dayjs('--1623787200--', '--X--', true).format('/[Date](X)/') // Invalid Date
dayjs('1623787200', 'X', true).format('X') // 1623787200
dayjs('1623787200', 'X', true).format('YYYY-MM-DD') // 2021-06-16

Expected behavior
not to get Invalid Date

Information

  • Day.js Version 1.10.6
  • Node version: 14.17.0
  • Time zone: [e.g. GMT +4:00]

related #1533 #1567

@iamkun
Copy link
Owner

iamkun commented Jul 7, 2021

I see.

Cause x X format is supported via advancedFormat plugin, you may enable it to use in strict mode

https://day.js.org/docs/en/plugin/advanced-format

@safareli
Copy link
Author

safareli commented Jul 7, 2021

It's enabled. look at the collapsed "source" section of the issue I have this there:

const dayjs = require('dayjs');
dayjs.extend(require('dayjs/plugin/customParseFormat'))
dayjs.extend(require('dayjs/plugin/advancedFormat'))

@iamkun
Copy link
Owner

iamkun commented Jul 7, 2021

fixed in #1571

@safareli
Copy link
Author

Any plans of making a release with the change?

1 similar comment
@iamoldli
Copy link

Any plans of making a release with the change?

@iamkun
Copy link
Owner

iamkun commented Sep 10, 2021

🎉 This issue has been resolved in version 1.10.7 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging a pull request may close this issue.

3 participants