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.

Daylight saving is not being considered while using date-fns

See original GitHub issue

The offset is not considering for pacific timezone. Right now we are in PDT but the timezone always formats the time according to PST. Rather than using GMT-07:00 rather I always end up getting GMT-08:00. The example is mentioned below

require("date-fns/package.json"); // date-fns is a peer dependency. 
var dateFnsTz = require("date-fns-tz")

const laTimeZone = 'America/Los_Angeles'
const laDate = dateFnsTz.utcToZonedTime(new Date('2020-02-02T06:59:00z'), laTimeZone);
console.log(laDate)

// WRONG RESULT - Sat Feb 01 2020 22:59:00 GMT-0800 (Pacific Standard Time)

// CORRECT RESULT - Sat Feb 01 2020 23:59:00 GMT-0700 (Pacific Standard Time)

Is there any way to accommodate the DST along with the timezone

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:17
  • Comments:11

github_iconTop GitHub Comments

5reactions
shivkumarganeshcommented, Jan 19, 2021

We are having almost the same problem, but when converting zonedTimeToUtc:

const date = new Date("2020-10-19T00:00:00.000Z") // This is Madrid local time zone, I want the UTC
const utcDate = zonedTimeToUtc(date, 'Europe/Madrid');
console.log(utcDate.toISOString()) // 2020-10-18T23:00:00.000Z

This is wrong as it should be 2020-10-18T22:00:00.000Z, -1 for being in Madrid and -1 for being in DST.

Use parse in order to parse a date do not use new Date. new Date actually attaches the timezone information alongside and that kinda messes things up. Use ‘yyyy-MM-dd'T'HH:mm:ss.SSSz’ to parse your date and then do the required. Let me know if you find proper results.

You can refer https://runkit.com/shivkumarganesh/daylightsavingissue

var df = require("date-fns") var dateFnsTz = require("date-fns-tz") const parsedDate = df.parse("2020-10-19T00:00:00.000Z","yyyy-MM-dd\'T\'HH:mm:ss.SSS\'Z\'",new Date()); const utcDate = dateFnsTz.zonedTimeToUtc(parsedDate, 'Europe/Madrid'); console.log(utcDate.toISOString()) // 2020-10-18T22:00:00.000Z

5reactions
DanielRamosAcostacommented, Oct 20, 2020

We are having almost the same problem, but when converting zonedTimeToUtc:

const date = new Date("2020-10-19T00:00:00.000Z") // This is Madrid local time zone, I want the UTC
const utcDate = zonedTimeToUtc(date, 'Europe/Madrid');
console.log(utcDate.toISOString()) // 2020-10-18T23:00:00.000Z

This is wrong as it should be 2020-10-18T22:00:00.000Z, -1 for being in Madrid and -1 for being in DST.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add a day via date-fns considering time zone change
UTC doesn't have daylight saving, nor is it a timezone. When you add a day using local methods and cross a daylight saving...
Read more >
Date-fns is returning incorrect dates - The freeCodeCamp Forum
Daylight saving is not being considered while using date -fns​​ The offset is not considering for pacific timezone. Right now we are in...
Read more >
How to handle Time Zones in JavaScript - Bits and Pieces
With almost 140 functions, Date-fn is referred to be Lodash for dates. Date-fns can have the following advantages : Immutable and pure; Native ......
Read more >
date-fns - modern JavaScript date utility library
date -fns provides the most comprehensive yet simple and consistent toolset for manipulating JavaScript dates in a browser & Node.js.
Read more >
Date.prototype.getTimezoneOffset() - JavaScript | MDN
In a region that annually shifts in and out of Daylight Saving Time (DST), as date varies, the number of minutes returned by...
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