dayjs icon indicating copy to clipboard operation
dayjs copied to clipboard

dayjs doesn't say that the invalid date is invalid (eg 2025-36-36T14:19:39) (ISO format)

Open ghost opened this issue 8 months ago • 3 comments

Describe the bug When initializing dayjs with the invalid date (day 40 or month 13), it doesn't say that the date is invalid.

example:

dayjs('2025-13-36T14:19:39')
// Thu, 05 Feb 2026 12:19:39 GMT

Expected behavior date.isValid should return false.

Information

  • Day.js Version [e.g. v1.11.13]
  • Framework: Nodejs v22.15.0
  • Time zone: GMT+02:00 DST

ghost avatar May 04 '25 15:05 ghost

I am the author of this issue, I've done some migrations between accounts, so I am leaving this comment here so that you'll be able to communicate with me and so that I can keep track of this issue.

8o8inCodes avatar May 05 '25 13:05 8o8inCodes

By default, Day.js uses a lenient parsing mode and will try to parse a datetime input as much as possible, even if the values are not strictly valid.

If you need strict parsing, you may have to:

  1. Tell Day.js the exact format to parse.
  2. Enable the strict flag by passing true as the third argument.
import dayjs from 'dayjs'
import customParseFormat from 'dayjs/plugin/customParseFormat'

dayjs.extend(customParseFormat)

const strictDate = dayjs('2025-13-36T14:19:39', 'YYYY-MM-DDTHH:mm:ss', true)
console.log(strictDate.isValid()) // false

iamkun avatar Sep 09 '25 02:09 iamkun

@iamkun Don't you think that there should be a separate flag for it in addition? Like in the pr I've provided? We have a use case where users are using free format, but not allowed to use lenient dates. Right now I've added a custom plugin that would do this, but not sure if this should be natively supported or not.

What do you think?

8o8inCodes avatar Oct 09 '25 10:10 8o8inCodes