iamkun / iamkun/dayjs

format('x') result is different from moment

Open
#2,546 0 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**
see code below:

```js
const dayjs = require("dayjs");
const utc = require("dayjs/plugin/utc");
const timezone = require("dayjs/plugin/timezone");
const advancedFormat = require("dayjs/plugin/advancedFormat");

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

const date = dayjs(1704437980110).tz("Asia/Tokyo");
console.log(date.format("x"));
console.log(date.utc().format("x"));

// result:
// 1704441580110 // <== wrong
// 1704437980110
```

```js
const moment = require("moment");
const momentTz = require("moment-timezone");

const date = moment(1704437980110).tz("Asia/Tokyo");
console.log(date.format("x"));
console.log(date.utc().format("x"));

// result:
// 1704437980110
// 1704437980110
```

**Expected behavior**
Get same result with dayjs.

**Information**
- Day.js Version [e.g. v1.11.10]
- OS: [macOS]
- Browser [ 120.0.6099.1292]
- Time zone: ['Asia/Shanghai']

**Others**
`$d` is changed after `tz('xxx')`, so `$d.getTime()` caused this.

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the issue with the provided Day.js utc, timezone, and advancedFormat setup, focusing on format('x') after tz('Asia/Tokyo') and after utc(). Trace the timezone and timestamp-formatting entry points implicated by the reproduction. Done means the local-time and UTC format('x') results match the original timestamp, as in Moment.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
api
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.