dayjs().startOf('day') shifts two hours back
Asked Answered
S

2

11

I'd like to sort out a dayjs problem I am experiencing. Right now, I'm out of clues and/or guesses of what can happen, apart from the bad timezone setting. But again, how could timezone play a role here, if I didn't even modify it anywhere in my code?

I'm having a dayjs object, and I want to get the beginning of the day with it. When printing the output into console, I get:

d {
  '$L': 'en',
  '$d': 2021-03-18T22:00:00.000Z,
  '$x': {},
  '$y': 2021,
  '$M': 2,
  '$D': 19,
  '$W': 5,
  '$H': 0,
  '$m': 0,
  '$s': 0,
  '$ms': 0
}

Efficiently that means that month, day, week, hour, minute, second, and millisecond options are set correctly (they are all zero), but the date field in $d is absolutely wrong (it's shifted 2 hours back for some reason). How do I make the date update correctly as well? Please explain why does it happen so I will know how to deal with it in the future.

Using node.js with express.js to run day.js on. Node version is 15.8.0 Express version is 4.17.1 Dayjs version is 1.10.4

Many thanks in advance!

Sucker answered 19/3, 2021 at 9:25 Comment(5)
Presumably you're in UTC+2? Then the start of your day is 10pm the previous day in UTC.Yong
I myself am in GMT+2. Does it return UTC start of the day by default?Sucker
No, you can see it's referring to the start of your day, 2021-03-19T00:00:00.000+02:00. But in UTC, which is two hours behind, that's 2021-03-18T22:00:00.000Z Yong
That might be an issue if I move servers. Is there any way to make it always return YYY-MM-DDT00:00:00.000, as a start of a day without timezone in general, not the start of my day in particular (when converting to js date, dayjs takes exactly $d)?Sucker
Why are you logging the object and looking at its internal fields instead of logging the output of the format function?Lounge
A
18

(Sorry for the redaction, English is not my first language) I can answer with something that I did for my job, meanwhile some other person answer with maybe a better explanation.

In dayjs documentation there's a section for the UTC plugin there's a function called .utcOffset() which can be used to set the offset to the hours to 0, I used it because I had the same issue as you with 5 hours gap, so to obtain the date with hours in 0 I used it like this:

let today = dayjs().utcOffset(0).startOf('date').toDate();

and It gives me the date with hours, minutes and seconds in zero 2021-06-15T00:00:00.000Z

I hope it can be of help to someone.

Aalesund answered 15/6, 2021 at 17:22 Comment(1)
The function requires the UTC plugin to be added: import utc from 'dayjs/plugin/utc'; dayjs.extend(utc);Callable
A
-5

The version of dayjs - 1.10.4 seems to have an issue with the startof the date. As it generally shifts the startDate by 2-5 hours.

So it's better to use moment() in place of dayjs(). I used the moment() - v2.29.1 and the issue was resolved.

instead of - let dateValue = dayjs(startDate)

use

let dateValue = moment(startDate)

Abridge answered 13/4, 2023 at 12:1 Comment(1)
Moment is a legacy project and should not be used anymore. See: momentjs.com/docs/#/-project-status Either way, recommending a different tool is not a good answer to the question.Victoriavictorian

© 2022 - 2025 — McMap. All rights reserved.