question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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 https://github.com/iamkun/dayjs/issues/1533 #1567

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
iamkuncommented, Sep 10, 2021

🎉 This issue has been resolved in version 1.10.7 🎉

The release is available on:

Your semantic-release bot 📦🚀

0reactions
iamoldlicommented, Jul 23, 2021

Any plans of making a release with the change?

Read more comments on GitHub >

github_iconTop Results From Across the Web

new Date() returning invalid date unless * by 1?
The reason behind this is how new Date interprets its arguments. Looking at the relevant prototypes we see: new Date(value).
Read more >
Bash Reference Manual
Parses the tokens into simple and compound commands (see Shell Commands). Performs the various shell expansions (see Shell Expansions), breaking ...
Read more >
Fix List for Rational Business Developer
This document contains a complete listing of releases, refreshes, fix packs and interim fixes sorted by version for IBM Rational Business Developer.
Read more >
Issues Addressed in SAS 9.4 (TS1M7)
53105, A SAS ® program to send e-mail fails and returns the error "501 5.1.7 Invalid address " when the SMTP server does...
Read more >
Version News for Windows
Dropbox FS: OAuth token handling now handles the expiring Refresh Tokens. ... Sync Tree Visual: Fixed Analyze did not switch View Mode to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found