iamkun / dayjs

⏰ Day.js 2kB immutable date-time library alternative to Moment.js with the same modern API
https://day.js.org
MIT License
46.81k stars 2.29k forks source link

Incorrect parsing of fractional seconds in ISO-8601 when no timezone is specified #1923

Open benj-dobs opened 2 years ago

benj-dobs commented 2 years ago

Describe the bug DayJS thinks that the time '00:00:00.5' is five milliseconds after midnight, but it should be 500 milliseconds after midnight.

Reproduction:

const dayjs = require('dayjs');
const dateWithoutSpecifiedTimezone = dayjs('2000-01-01T00:00:00.5');
const dateInUTC = dayjs('2000-01-01T00:00:00.5Z');

dateInUTC.toISOString(); // '2000-01-01T00:00:00.500Z' - Correct
dateWithoutSpecifiedTimezone.toISOString(); // '2000-01-01T00:00:00.005Z' - Incorrect - should be '2000-01-01T00:00:00.500Z'

Expected behavior It should interpret the number of seconds ss.SSS as a decimal fraction, no matter how many decimal places are given, regardless of whether there is a timezone specified

Information

BePo65 commented 2 years ago

In this regard dayjs is consistent to what moment does. If we look at the documentation of moment we see:

Note that the number of S characters provided is only relevant when parsing in strict mode. In standard mode, S, SS, SSS, SSSS are all equivalent, and interpreted as fractions of a second. For example, .12 is always 120 milliseconds, passing SS will not cause it to be interpreted as 12 milliseconds.

So IMHO the result of your example is correct, as dayjs tries to be as close to moment as possible (without sacrificing the size).

benj-dobs commented 2 years ago

@BePo65 I think that documentation is describing the behaviour I think is correct, not the behaviour I'm observing.

Moment and Dayjs do not behave the same out-of-the-box in this case:

https://codesandbox.io/s/interesting-hill-6lji80?file=/src/index.js

BePo65 commented 2 years ago

Oh, I missed the output from dayjs 😳

I created a pr for this (pr #1936)

benj-dobs commented 2 years ago

Amazing, thank you!