moment / luxon

⏱ A library for working with dates and times in JS
https://moment.github.io/luxon
MIT License
15.4k stars 729 forks source link

Strings created with toIso() contain milliseconds #1381

Closed codingfabi closed 1 year ago

codingfabi commented 1 year ago

When formatting a date with the toIso() function, the resulting date contains milliseconds even though the ISO 8601 definition does not consider milliseconds ( seen here https://en.wikipedia.org/wiki/ISO_8601).

I found this issue: #253 where I could see that milliseconds in the ISO format were purposefully implemented. However, I either do not understand the reason or would argue that even though fractional seconds are not cut from the ISO date, they should not be part of the displayed string.

So I would assume two ways to approach this:

I'm also happy if this is not implemented but would be really interested in the reason for the necessity of the miliseconds :)

diesieben07 commented 1 year ago

the ISO 8601 definition does not consider milliseconds

I am not sure where you got this information. The Wikipedia article you linked specifically states that:

A decimal fraction may be added to the lowest order time element present in any of these representations. A decimal mark, either a comma or a dot, is used as a separator between the time element and its fraction.

This is exactly what Luxon does when ISO-formatting a timestamp with sub-second components (i.e. milliseconds):

console.log(DateTime.fromObject({milliseconds: 300}).toISO());

Outputs: "2023-02-20T12:20:46.300+01:00"

You can suppress the output of the milliseconds (if there aren't any, which you can ensure using startOf('second')) by using toISO({suppressMilliseconds: true}).

codingfabi commented 1 year ago

I am not sure where you got this information. The Wikipedia article you linked specifically states that:

The wikipedia entry for ISO8601 shows the datestring without milliseconds, thats why I assumed the stringified ISO dates would be shown without sub-seconds aswell: Screenshot 2023-02-20 at 15 23 33

You can suppress the output of the milliseconds (if there aren't any, which you can ensure using startOf('second')) by using toISO({suppressMilliseconds: true}).

Yeah thank you, this is all I needed :) Closing this as it does not require any actions.