dayjs icon indicating copy to clipboard operation
dayjs copied to clipboard

fix: .format('YYYY') returns less than 4 digits for years < 1000

Open notoriousmango opened this issue 3 years ago • 3 comments
trafficstars

close #1745

notoriousmango avatar Jun 19 '22 00:06 notoriousmango

Codecov Report

Merging #1953 (4b14ff6) into dev (cbe91fd) will not change coverage. The diff coverage is n/a.

:exclamation: Current head 4b14ff6 differs from pull request most recent head fb0db64. Consider uploading reports for the commit fb0db64 to get more accurate results

@@            Coverage Diff            @@
##               dev     #1953   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          183       183           
  Lines         2096      2096           
  Branches       550       550           
=========================================
  Hits          2096      2096           
Impacted Files Coverage Δ
src/index.js 100.00% <ø> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

codecov[bot] avatar Jun 27 '22 16:06 codecov[bot]

I had the same problem and came up with the same solution.


The documentation clearly says: https://github.com/dayjs/dayjs-website/blob/8336b896f5584f161ac99ed502ede6ad21bc74f6/docs/display/format.md?plain=1#L22-L25

Format Output Description
YY 18 Two-digit year
YYYY 2018 Four-digit year

So right now, the code does not conform to the documentation. But this PR fixes that.


As an improvement, I would just add a Y format that keeps the original value of this.$y, just in case, and reuse Utils.s(this.$y, 2, '0') in YY for consistency.

  Y: this.$y,
  YY: Utils.s(this.$y, 2, '0'),
  YYYY: Utils.s(this.$y, 4, '0'),

sylann avatar Aug 10 '22 17:08 sylann

@iamkun can you please review this

notoriousmango avatar Aug 10 '22 21:08 notoriousmango

@iamkun Can you please review this?

notoriousmango avatar Nov 06 '22 03:11 notoriousmango