最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - dayjs().startOf('day') shifts two hours back - Stack Overflow

programmeradmin1浏览0评论

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!

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!

Share Improve this question asked Mar 19, 2021 at 9:25 RusuranoRusurano 5521 gold badge5 silver badges16 bronze badges 5
  • Presumably you're in UTC+2? Then the start of your day is 10pm the previous day in UTC. – jonrsharpe Commented Mar 19, 2021 at 9:30
  • I myself am in GMT+2. Does it return UTC start of the day by default? – Rusurano Commented Mar 19, 2021 at 9:45
  • 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 – jonrsharpe Commented Mar 19, 2021 at 9:49
  • 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)? – Rusurano Commented Mar 19, 2021 at 9:54
  • Why are you logging the object and looking at its internal fields instead of logging the output of the format function? – Matt Johnson-Pint Commented Mar 19, 2021 at 21:54
Add a comment  | 

2 Answers 2

Reset to default 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.

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)

发布评论

评论列表(0)

  1. 暂无评论