iamkun / iamkun/dayjs

utc() gives inconsistent results when given an array of formats to parse

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

Description

**Describe the bug**
```
const a = dayjs
.utc('2020-12-30T17:00:00Z', 'YYYY-MM-DDTHH:mm:ss[Z]')
.tz('America/New_York');
const b = dayjs
.utc('2020-12-30T17:00:00Z', ['YYYY-MM-DDTHH:mm:ss[Z]'])
.tz('America/New_York');
console.log({
'a.format()': a.format(),
'b.format()': b.format(),
});
```
gives
```
{
'a.format()': '2020-12-30T12:00:00-05:00',
'b.format()': '2020-12-30T17:00:00-05:00'
}
```

**Expected behavior**
`a.format()` and `b.format()` should return the same result. This is weird because `dayjs.utc()` behaves like `dayjs()`, such as accepting formats as a list and strict parsing:
```
console.log(
dayjs
.utc('2020-12-30T17:00:00Z', ['YYYY-MM-DD', 'YYYY-MM-DD HH:mm'], true)
.isValid(),
); // false
```

**Information**
- Day.js Version 1.11.9
- OS: Mac OS
- Browser: Google Chrome v115
- Time zone: Eastern Daylight Time

Contributor guide

Open the contributing guide

Research direction

Start by running the two utc() examples from the issue and compare their timezone-converted output. Trace the parsing path for a single format versus an array, including the strict-parsing example, and add or update coverage for equivalent results; done means both calls produce the same formatted value.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.