iamkun / iamkun/dayjs

Is it possible to parse only valid Date strings according to ISO8601

Open
#1,729 1 comment 15 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
48.7k
Forks
2.5k
PR merge metrics
No merged PRs in 30d

Description

It is possible to check whether string according ISO8601 is a valid date?

For example `dayjs('1970-00-00', 'YYYY-MM-DD', true).isValid()` should return `false` in my opinion, because because month 0 or day 0 do not exist.

Providing all possible date formats according ISO8601 is cumbersome and would look like this

```
dayjs('2021-12-01T00:00:01', [
'YYYY', 'YYYY-MM', 'YYYY-MM-DD',
'YYYY-MM-DDTHH', 'YYYY-MM-DDTHH:mm', 'YYYY-MM-DDTHH:mm:ss', 'YYYY-MM-DDTHH:mm:ss.S', 'YYYY-MM-DDTHH:mm:ss.SS', 'YYYY-MM-DDTHH:mm:ss.SSS',
'YYYY-MM-DDTHHZ', 'YYYY-MM-DDTHH:mmZ', 'YYYY-MM-DDTHH:mm:ssZ', 'YYYY-MM-DDTHH:mm:ss.SZ', 'YYYY-MM-DDTHH:mm:ss.SSZ', 'YYYY-MM-DDTHH:mm:ss.SSSZ',
'YYYY-MM-DDTHHZZ', 'YYYY-MM-DDTHH:mmZZ', 'YYYY-MM-DDTHH:mm:ssZZ', 'YYYY-MM-DDTHH:mm:ss.SZZ', 'YYYY-MM-DDTHH:mm:ss.SSZZ', 'YYYY-MM-DDTHH:mm:ss.SSSZZ'
], true).isValid()

```

However, time zone can be provided as UTC offset, e.g, `+02:30`, `+02`, `+0230`, `+2` but also as `Z` indicating UTC time. The [List of all available parsing tokens](https://day.js.org/docs/en/parse/string-format#list-of-all-available-parsing-tokens) apparently does not support `Z` for UTC.

Can you add proper support for ISO8601 date formats?

Wernfried

Contributor guide

Open the contributing guide

Research direction

Start by reviewing Day.js's strict parsing behavior and the ISO8601 formats and offsets listed in the issue. Compare the current handling of month/day zero, Z, and UTC offsets against the requested formats; done means valid ISO8601 strings parse correctly and invalid dates are rejected.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
api
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.