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.

DayJS parses wrong year value for the date "0001-01-01"

See original GitHub issue

Describe the bug When trying to parse the dates “0000-01-01” and “0001-01-01” DayJS returns the years 1900 and 1901 respectively:

const dayjs = require('dayjs')
console.log(dayjs("0001-01-01", "YYYY-MM-DD"))

Output:

d {
  '$L': 'en',
  '$d': 1901-01-01T03:06:28.000Z,
  '$x': {},
  '$y': 1901,
  '$M': 0,
  '$D': 1,
  '$W': 2,
  '$H': 0,
  '$m': 0,
  '$s': 0,
  '$ms': 0
}

Expected behavior The expected behavior is that the returned object has the correct year. Setting the year manually we can see the correct behavior working:

const dayjs = require('dayjs')
console.log(dayjs("2020-01-01", "YYYY-MM-DD").year(1))

Output:

d {
  '$L': 'en',
  '$d': 0001-01-01T03:06:28.000Z,
  '$x': {},
  '$y': 1,
  '$M': 0,
  '$D': 1,
  '$W': 1,
  '$H': 0,
  '$m': 0,
  '$s': 0,
  '$ms': 0
}

Information

  • Day.js Version v1.9.6
  • OS: Windows
  • Browser: N/A (Node.js v12.18.3)
  • Time zone: GMT-03:00

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:16 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
shye0000commented, Apr 19, 2022

This might be useful

Date - Two digit years map to 1900 – 1999

let date = new Date(98, 1)  // Sun Feb 01 1998 00:00:00 GMT+0000 (GMT)

// Deprecated method; 98 maps to 1998 here as well
date.setYear(98)            // Sun Feb 01 1998 00:00:00 GMT+0000 (GMT)

date.setFullYear(98)        // Sat Feb 01 0098 00:00:00 GMT+0000 (BST)

@iamkun yo, just created a PR to fix this issue in which I refactored a bit the parseDate method using the method setFullYear inspired by @naulacambra

all tests has passed, could you plz review this PR? we really need this fix for our project, thx in advance.

3reactions
AHBrunscommented, Dec 12, 2021

So, we just can’t use this with a regular date input without writing a custom guard?

const date = countLeadingZeroes(dateString.split("-")[2] ?? "0000")) === 0 ? dayjs(dateString) : null

Seems pretty annoying considering the whole point of a date lib is to prevent having to parse and manipulate dates manually. Even more so when this is a problem for anyone who parses a date input’s value.

Read more comments on GitHub >

github_iconTop Results From Across the Web

dayjs returns wrong date with format - javascript - Stack Overflow
Try date = '1989-08-12' . What will be the result? · Your problem is your timezone .format format with your timezone. · 1....
Read more >
Validation
Returns a boolean indicating whether the Dayjs 's date is valid. Non-strict check. Only checks if the value could be parsed to a...
Read more >
Parse, Validate, Manipulate, and Display Dates and Times in ...
Parsing a date and time string into a Day.js object is easy and supports strings, numbers, native JavaScript Date objects as well as...
Read more >
Connect CDC v5.8 System Reference Guide
The following expression gets a PARSE ERROR because of an improper use of a ... does: Convert DB2 dates with the value of...
Read more >
News in the BRFplus Formula - SAP Blogs
A new formula parser has been written to better analyze the ... DT_IS_NULL, Returns TRUE if a Timepoint is NULL (date part is...
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