iamkun / iamkun/dayjs

Time-only TZ Parse Returns Wrong Date When Server Date is Different than TZ

Open
#2,091 4 comments 0 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**
When parsing a time-only string using a timezone aware parse, it returns the wrong date when the server is in a different date than the timezone

For example, when it's 22:00 CDT and 03:00 UTC (+1 day)
```
let dt = dayjs.tz("5:59 PM", "h:mm A", "America/Chicago");
```

```
Local: 2022-10-21T22:00:00-05:00
UTC: 2022-10-22T03:00:00+00:00

Parsed: 2022-10-22T17:59:00-05:00
```

**Expected behavior**
The inferred date should be timezone aware rather than use the server date.

The parsed datetime should thus be:
```
Parsed: 2022-10-21T17:59:00-05:00
```

**Information**
- Day.js Version 1.11.6
- Node: 16.x
- Server Timezone: UTC
- Input Timezone: anything with a different date than UTC at the time of execution

We allow users to enter date expressions to be evaluated by specifying their own input value, format, and timezone... thus they would reasonably expect the _time_ to be parsed in the _date_ of their timezone. As a workaround, we recommend that users inject the current _date_ into their string for parsing, but this is a confusing workaround for novice users.

### Full Example
https://codesandbox.io/s/hungry-bassi-w1ukl7?file=/src/index.js
```
const dayjs = require("dayjs");
var utc = require("dayjs/plugin/utc");
var timezone = require("dayjs/plugin/timezone");
var customParseFormat = require("dayjs/plugin/customParseFormat");

dayjs.extend(utc);
dayjs.extend(timezone);
dayjs.extend(customParseFormat);

let dt = dayjs.tz("5:59 PM", "h:mm A", "America/Chicago");

//this breaks when the UTC date is different than the local time
// eg. local=2022-10-21 CDT
// utc=2022-10-22 UTC
let localTime = dayjs().tz("America/Chicago");
let utcTime = dayjs();

let results = `Local: ${localTime.format()}
UTC: ${utcTime.format()}
Parsed is: ${dt.format()}`;

console.log(results);
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.