Parsing with Unix Timestamp tokens (X/x) and other visual tokens in strict mode results in Invalid Date
See original GitHub issueDescribe 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]
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

🎉 This issue has been resolved in version 1.10.7 🎉
The release is available on:
Your semantic-release bot 📦🚀
Any plans of making a release with the change?