startOf/endOf misbehave with timezone plugin enabled
See original GitHub issueDescribe the bug startOf/endOf returns the wrong value with timezone plugin enabled. The information about the timezone gets lost
const dayjs = require("dayjs")
const utc = require("dayjs/plugin/utc");
const timezone = require("dayjs/plugin/timezone");
dayjs.extend(utc);
dayjs.extend(timezone);
console.log(dayjs.tz("2020-11-02T00:00:00", "Europe/Berlin").startOf("month").toDate());
// Output: 2020-11-01T00:00:00.000Z
Expected behavior
The expected output to code above is imho 2020-10-31T23:00:00.000Z, which relates to 2020-11-01T00:00:00 in Europe/Berlin
Workaround
We looked into the code and found the .toDate() parameter. The code works as expected when calling .toDate("s").
console.log(dayjs.tz("2020-11-02T00:00:00", "Europe/Berlin").startOf("month").toDate("s"));
// Output: 2020-10-31T23:00:00.000Z
Information
- Day.js Version v1.10.6
- OS: browser/node
- Browser latest chrome
- Time zone: see above
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:5 (2 by maintainers)
Top Results From Across the Web
startOf/endOf misbehave with timezone plugin enabled #1573
Describe the bug startOf/endOf returns the wrong value with timezone plugin enabled. The information about the timezone gets lost
Read more >5.1.15 MySQL Server Time Zone Support
This section describes the time zone settings maintained by MySQL, ... how to stay current with time zone changes, and how to enable...
Read more >Time zones - Product Documentation | ServiceNow
All times are stored in Coordinated Universal Time (UTC) and appear globally based on the system time zone. However, times appear to users ......
Read more >Timezone
Timezone adds dayjs.tz .tz .tz.guess .tz.setDefault APIs to parse or display between time zones. var utc = require('dayjs/plugin/utc') var timezone ...
Read more >Changing a DAG's timezone on Amazon MWAA
Create a plugin to change the timezone in Airflow logs. Apache Airflow will run the Python files in the plugins directory at start-up....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Hello, just stumbled upon the same bug and it doesn’t seem related to the system’s timezone. Here’s an easily reproducible snippet.
Here’s the context:
Example: User input: 2021-06-18 00:00:00 Europe/Paris Expected output: 2021-06-18 23:59:59 Europe/Paris -> 2021-06-18 21:59:59 UTC
Instead of using endOf(), doing this “by hand” actually works (but requires first ensuring hour input is 00:00:00):
@imwh0im it’s probably the code from your comment. @flashspys it works for me if I use the internal Date object $d: index.js:
toDate() { return new Date(this.$d); }But then utc-utcOffset.test fails.