0

I am facing an issue while handling dates in Javascript/Typescript, where Day light saving is not observed like countries in India..

The code is working fine in countries like Canada, US where Day light saving is observed.

Date.prototype.stdTimezoneOffset = function() {
  var jan = new Date(this.getFullYear(), 0, 1);
  var jul = new Date(this.getFullYear(), 6, 1);
  return Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
}
Date.prototype.dst = function() {
  return (this.getTimezoneOffset()) < this.stdTimezoneOffset();
}

P.S: Change your computer time zone to India Time zone while test.

Below are the links for Angular and Javascript resp.

https://stackblitz.com/edit/hello-angular-6-psxfnf?file=src/app/app.component.ts

https://jsfiddle.net/oc1uefbq/

Date is not getting handled properly.. for month like October

October 14 shall not represented as October 13..(In Angular code)

Gags
  • 3,759
  • 8
  • 49
  • 96
  • I'm not sure what output you are expecting and what output you are getting? I live in a time zone that does not have DST. the `dst` function you give reports false for January, July, and October. – Pace Dec 01 '19 at 08:59
  • If you check Angular code.. then Oct 14 is represented as Oct 13 – Gags Dec 01 '19 at 14:47
  • 1
    FYI, code seems to come from [this other question](https://stackoverflow.com/questions/11887934/how-to-check-if-the-dst-daylight-saving-time-is-in-effect-and-if-it-is-whats), with some names changed to be more confusing. – Álvaro González Dec 01 '19 at 15:28
  • I am not sure what you are referring to @ÁlvaroGonzález ... let me know if i can make things more clear – Gags Dec 01 '19 at 15:30
  • If you click on the link you'll see another question in Stack Overflow, the accepted answer of which contains your very same code, with `dst` renamed to `isDstObserved`. I shared my findings in case that could help others to figure out what your code is trying to do (you just say "handle DST"). – Álvaro González Dec 01 '19 at 15:34
  • but even that code does not work in countries where DST is not observed,, – Gags Dec 01 '19 at 15:39
  • Ok, so you pass in `October, 14 2019 00:00:00 -0400`. If the current time zone is in DST you convert to EST. If it is not you convert it into EDT correct? Well, that time, in EDT, is `October, 13 2019 23:00:00`. So it displays October 13. – Pace Dec 01 '19 at 18:11
  • but how can i make sure that it does not change date? .. this works fine if i change my timezone to US.. and it gives October 13 when i change time zone to India. How can i get uniform results no matter of which timezone – Gags Dec 01 '19 at 23:05
  • What kind of uniform results? I’m still not sure what your desired outcome is. If you display an instant at different time zones that instant may be reported as different days. Currently it is December 2nd in India time and December 1st in my time zone. – Pace Dec 02 '19 at 06:34

0 Answers0