question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

startOf/endOf misbehave with timezone plugin enabled

See original GitHub issue

Describe 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:open
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
bpolaszekcommented, Nov 9, 2021

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:

  • User input is Europe/Paris (+2h from UTC in summer)
  • Required output is UTC
  • I want to have a date at 23:59:59, converted to UTC.

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

const input = dayjs.tz('2021-06-18 00:00', 'Europe/Paris');
console.log(input.endOf('day').format('YYYY-MM-DDTHH:mm:ssZ[Z]')); // 2021-06-18T23:59:59+02:00Z - correct
console.log(input.endOf('day').tz('UTC').format('YYYY-MM-DDTHH:mm:ssZ[Z]')); // 2021-06-18T20:59:59+00:00Z - invalid

Instead of using endOf(), doing this “by hand” actually works (but requires first ensuring hour input is 00:00:00):

const input = dayjs.tz('2021-06-18 00:00', 'Europe/Paris');
console.log(input.add(1, 'day').subtract(1, 'second').format('YYYY-MM-DDTHH:mm:ssZ[Z]')); // 2021-06-18T23:59:59+02:00Z - correct
console.log(input.add(1, 'day').subtract(1, 'second').tz('UTC').format('YYYY-MM-DDTHH:mm:ssZ[Z]')); // 2021-06-18T21:59:59+00:00Z - valid
0reactions
kmcscommented, Jul 16, 2021

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found