Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

startOf/endOf misbehave with timezone plugin enabled #1573

Open
Kanatorabo opened this issue Jul 12, 2021 · 5 comments
Open

startOf/endOf misbehave with timezone plugin enabled #1573

Kanatorabo opened this issue Jul 12, 2021 · 5 comments

Comments

@Kanatorabo
Copy link

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
@imwh0im
Copy link
Contributor

imwh0im commented Jul 13, 2021

has no issue in my case

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());  // 2020-10-31T23:00:00.000Z

Day.js Version: 1.10.6
Time zone: Asia/Seoul

@flashspys
Copy link

@imwh0im Indeed. The default system TZ is part of this problem.

TZ=UTC node ./the-code-you-posted-above.js // 2020-10-31T23:00:00.000Z <- correct
TZ=Europe/Berlin node ./the-code-you-posted-above.js // 2020-11-01T00:00:00.000Z <- wrong

@imwh0im
Copy link
Contributor

imwh0im commented Jul 14, 2021

@flashspys Should the two result values be the same? I'm asking because I don't know the script in the the-code-you-posted-above.js.

@kmcs
Copy link

kmcs commented 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.

@bpolaszek
Copy link

bpolaszek commented 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants