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.

Default year should be the current year instead of 2001

See original GitHub issue

Describe the bug If we parse the date “12/30” (e.g. dayjs("12/30")), dayjs uses 2001 as the default year:

dayjs("12/30").format() //Yields "2001-12-30T00:00:00-07:00"

Expected behavior The most logical and useful behavior is for dayjs to use the current year as the default year.

  1. It’s natural and human-like. When humans write dates, if they omit the year, it’s implied that the date applies to the current year. No human would think, “I’m writing a date in 2001, so I can omit the year”. The notion that year-less dates should default to the “first” year (2001, strangely) is a computer-centric approach.

  2. It’s awkward to implement otherwise. If datejs defaults the year to 2001 (instead of the current year), we may be inclined to do something like the following to change all dates from 2001 to the current year:

let s = "12/30";
let d = dayjs(s);
if(d.year() === 2001) {
  d = d.year(dayjs().year());
}
d.format(); //Yields "2020-12-30T00:00:00-07:00"

The huge flaw in this approach is that we’ve now lost the ability to parse legitimate dates from 2001. They will be automatically “converted” to their 2020 equivalent.

Another approach is to process the original string, detecting if a year is present, and appending the current year if one isn’t present yet. However, with so many different formats, that’s a tough bit of code to implement with lots of edge cases. This wouldn’t be a show-stopper except for the fact that the whole point of using a library like dayjs is to simplify this kind of date parsing.

  1. It looks like Moment.js may already have this feature.

Information

  • Day.js Version 1.9.6
  • OS: macOS
  • Browser: Node 14.1.0
  • Time zone: GMT-7

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
rinogocommented, Dec 7, 2020

Hi, @iamkun! Thank you for your response! Clearly, you are much more credentialed and qualified to comment on this issue than I am. However, I’m not sure I entirely agree that all dayjs can do is to guess. In my Issue submission, I tried to provide clear examples why using the current year (2020) is better than another, fixed year (2001).

In my particular use case, I can be confident that all 2001 dates should be transformed into their 2020 equivalents. So, I’m not concerned for my own application. However, I do believe it’s in the best interest of the dayjs library and its users to use the “current year” as the default.

Do you have specific pushback on the three points I mentioned above?

Ultimately, my opinion might be changed if someone could provide an alternative to the code provided in point 2 above that doesn’t prevent legitimate dates from 2001 from being used.

Thank you!

2reactions
iamkuncommented, Dec 5, 2020

Hi, there’s nothing right or wrong of returning 2001 or 2020. All dayjs could do is to guess. And new Date(‘12/03’) also returns 2001

So consistent results parsing anything other than ISO 8601 strings, you should use String + Format.

https://day.js.org/docs/en/parse/string-format

import dayjs from 'dayjs'
import customParseFormat from 'dayjs/plugin/customParseFormat'
dayjs.extend(customParseFormat)
console.log(dayjs('12/30', 'MM/DD').format());

Read more comments on GitHub >

github_iconTop Results From Across the Web

Moment.js default year is 2001 - javascript - Stack Overflow
The year defaults to 2001. Since the user may input the date in their own format, I didn't want to add in a...
Read more >
Change the date system, format, or two-digit year interpretation
You should be as specific as possible about dates whenever you enter them. This will produce ... Change the default date format to...
Read more >
MATLAB datestr - MathWorks
datestr considers two-character years (for example, '79' ) to fall within the 100-year range centered around the current year. All text representing dates...
Read more >
Date/Time Formats - IBM
The Date Formats global option changes the default date format for all maps or forms. However, the format of the existing date fields...
Read more >
The Greenhouse Gas Protocol
Instead companies need to make a good faith effort to provide a complete, accurate, ... year 2002 GHG emissions, and emissions associated with...
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