0

I have 2 questions.

1. Regarding daylight saving time implementation in JavaScript in browsers.

( Output is from Chrome Browser)

var dt1 = new Date(1975, 1 , 23, 1, 0, 0);
dt1.toString(); // Sun Feb 23 1975 01:00:00 GMT-0600 (Central Standard Time)
dt1.toUTCString(); // Sun, 23 Feb 1975 07:00:00 GMT
dt1.getTime(); // 162370800000 

var dt2 = new Date(1975, 1 , 23, 2, 0, 0);
dt2.toString(); // Sun Feb 23 1975 03:00:00 GMT-0500 (Central Daylight Time)
dt2.toUTCString(); // Sun, 23 Feb 1975 08:00:00 GMT
dt2.getTime(); //162374400000

var dt3 = new Date(1975, 1 , 23, 3, 0, 0);
dt3.toString(); // Sun Feb 23 1975 03:00:00 GMT-0500 (Central Daylight Time)
dt3.toUTCString(); // Sun, 23 Feb 1975 08:00:00 GMT
dt3.getTime(); // 162374400000

For the date objects dt2 and dt3, observe all the values are same. This is confusing.

2. Internet explorer has wrong daylight saving time start dates.

In year 1975, daylight saving time stared on date Feb 23 1975 2:00 AM. When I create date objects for dates Feb 23 1975 1AM, 2 AM and Apr 6 1975 1AM, 2AM, 3AM in Chrome and IE...

Feb 23 1975 1AM

Chrome: Sun Feb 23 1975 01:00:00 GMT-0600 (Central Standard Time)

IE : Sun Feb 23 1975 01:00:00 GMT-0600 (Central Standard Time)

Feb 23 1975 2AM

Chrome: Sun Feb 23 1975 03:00:00 GMT-0500 (Central Daylight Time)

IE : Sun Feb 23 1975 02:00:00 GMT-0600 (Central Standard Time)

Apr 6 1975 1AM

Chrome: Sun Apr 06 1975 01:00:00 GMT-0500 (CDT) //165996000000

IE : Sun Apr 06 1975 01:00:00 GMT-0600 (CST) //165999600000. Time in milliseconds is also different.

Apr 6 1975 2AM Chrome: Sun Apr 06 1975 02:00:00 GMT-0500 (CDT) // 165999600000

IE : Sun Apr 06 1975 01:00:00 GMT-0600 (CST) // 165999600000. Time in milliseconds is same.

Apr 6 1975 3AM

Chrome: Sun Apr 06 1975 03:00:00 GMT-0500 (CDT)

IE : Sun Apr 06 1975 03:00:00 GMT-0500 (CDT)

In IE for year 1975, daylight saving time logic starts after Apr 6 1975 2PM and not after Feb 23 1975 2AM as it is supposed to be. Chrome is working properly.

How do I show proper date string in all browsers when daylight saving time is involved?

benvc
  • 14,448
  • 4
  • 33
  • 54
user418836
  • 847
  • 3
  • 8
  • 19

1 Answers1

1

Daylight savings and timezones are the worst! :)

JavaScript Date/Time is always subject to the idiosyncrasies of each and every browser as it runs client side. So even 2 users with identical browsers may get variations based on their laptop settings.

To specifically answer the 2 questions in your post:

1: Looks like you are accessing the JavaScript from the Central Timezone which observes DST whereas UTC doesn't. Daylight savings time occurs at 2am, so dt1 will be in standard time as it is 1am on that that evening, where as dt2 & dt3 are in dst as they are 2am and 3am respectively. That's the reason for the difference.

2: Because Richard Nixon infamously mandated year-round daylight saving in 1974 and 1975. Appears the version of IE you are testing with handles the DST based on the executive mandate at the time, whereas Chrome applies it in a consistent format based on current guidelines.

Fortunately there is MomentJS and MomentTimezone which handles all these variations: Is moment.js browser independent?

These are essential JavaScript libraries for anything related to Date/Time
https://momentjs.com/
https://momentjs.com/timezone/

Easiest way to get going is:

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"> </script>
<script>
    var now = moment();
    console.log(now.format());
    console.log(now.isDST());
</script>

You can place that inside your bodytag and look in the console and it'll be working for you. Have fun!