react-spectrum icon indicating copy to clipboard operation
react-spectrum copied to clipboard

CalendarDate.toString() can return non ISO 8601 formatted strings

Open bk-mja opened this issue 2 weeks ago โ€ข 3 comments

Provide a general summary of the issue here

When adding a non integer number of days to a CalendarDate, the output of toString() is no ISO 8601 string anymore, but something like '2025-11-2.0000002'.

๐Ÿค” Expected Behavior?

someDate.toString()

always converts the date to an ISO 8601 formatted string (as per docs). For example '2025-10-28' .

๐Ÿ˜ฏ Current Behavior

someDate.toString()

can return strings like '2025-10-28.0000002', which is not conforming to ISO 8601 .

๐Ÿ’ Possible Solution

One possible solution would be to round to the nearest full day when calculating the output string.

Another way to solve this issue is not allowing non decimal values for year, month, and day in the first place by either throwing an error when given a non integer input, or rounding to the nearest number. However, im not sure what implication this would have. Throwing an error would certainly be a breaking change. As the CalendarDate object is supposed to represent date without any time components, I think not allowing decimal numbers by rounding them could be a reasonable solution here.

๐Ÿ”ฆ Context

I've ran into this issue in our production app, as i was feeding some drag and drop offset into the add function that had a rounding error. You could argue that it is the users responsibility to sanitize the input here, however i feel like this is never the intended behavior, so a fix on the library level feels appropriate. In my example I used the output of toString() to submit the resulting value to my backend which couldn't parse it back to a date.

๐Ÿ–ฅ๏ธ Steps to Reproduce

const dateA = new CalendarDate(2025, 10, 28);
const dateB = dateA.add({days: 0.0000002});
dateB.toString() // '2025-10-28.0000002'

CodeSandBox: https://codesandbox.io/p/devbox/angry-framework-8k5klq

Version

@internationalized/[email protected]

What browsers are you seeing the problem on?

Microsoft Edge

If other, please specify.

No response

What operating system are you using?

MacOS

๐Ÿงข Your Company/Team

No response

๐Ÿ•ท Tracking Issue

No response

bk-mja avatar Nov 28 '25 11:11 bk-mja