admin管理员组文章数量:1291534
In my project I use date-fns for date manipulations. There is a need to iterate days in some range. For that I'm using following code:
for (
// from and to are always start of some day, from <= to
let date = from;
isBefore(date, to) || isEqual(date, to);
date = addDays(date, 1)
) {
// Some operations with date
}
I'm expecting date
to always be the start of some day, but in case timezone changes (winter time -> summer time), date is 1 hour less then expected. Here is an example:
const from = new Date('2019-03-31T00:00:00.000Z')
const fromPlusDay = dateFns.addDays(from, 1)
// I'm getting "2019-03-31T23:00:00.000Z"
// instead of "2019-04-01T00:00:00.000Z"
fromPlusDay.toISOString()
By the way my time zone was +2 and after moving to summer time it became +3
In my project I use date-fns for date manipulations. There is a need to iterate days in some range. For that I'm using following code:
for (
// from and to are always start of some day, from <= to
let date = from;
isBefore(date, to) || isEqual(date, to);
date = addDays(date, 1)
) {
// Some operations with date
}
I'm expecting date
to always be the start of some day, but in case timezone changes (winter time -> summer time), date is 1 hour less then expected. Here is an example:
const from = new Date('2019-03-31T00:00:00.000Z')
const fromPlusDay = dateFns.addDays(from, 1)
// I'm getting "2019-03-31T23:00:00.000Z"
// instead of "2019-04-01T00:00:00.000Z"
fromPlusDay.toISOString()
By the way my time zone was +2 and after moving to summer time it became +3
Share asked Apr 7, 2019 at 19:41 dpsdps 8643 gold badges8 silver badges17 bronze badges 1- UTC doesn't have daylight saving, nor is it a timezone. When you add a day using local methods and cross a daylight saving boundary, the timezone changes and the UTC offset changes so you will see a difference in the hours (and possibly minutes) of the UTC timestamp. – RobG Commented Apr 7, 2019 at 21:03
2 Answers
Reset to default 3I faced with the same problem:
6 October 2019 in Australia is Daylight Saving Time Starts.
dh.parse('2019-10-06')
returns: Sat Oct 05 2019 23:00:00 GMT+1000 (Australian Eastern Standard Time)
Solution is adding info about time zone (one of):
- Add sign of absent time zone offset “Z” -- dh.parse('2019-10-06T00:00:00.000Z' )
- Add GMT -- dh.parse('2019-10-06 GMT' )
- Add +00:00 -- dh.parse('2019-10-06T00:00:00.000+00:00' )
]1)
It is considering your time zone change and it is at the start of day (in your timezone, not UTC). Midnight in UTC is not necessarily start of day in your zone.
Check this out (I'm in GMT+1 zone):
const from = dateFns.parse('2019-03-31') // -> "2019-03-31T00:00:00.000+0100"
const fromPlusDay = dateFns.addDays(from, 1)
dateFns.format(fromPlusDay , 'YYYY-MM-DDTHH:mm:ss.SSSZZ') // -> "2019-04-01T00:00:00.000+0200"
I think what you are doing is fine, just don't expect your zone to be 00:00
in UTC, and avoid printing dates in UTC.
本文标签: javascriptAdd a day via datefns considering time zone changeStack Overflow
版权声明:本文标题:javascript - Add a day via date-fns considering time zone change - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741534172a2383934.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论