The functionality of new Date() is inconsistent when encountering a daylight savings time switch

In my current situation, I am facing an issue when passing "year, month, date, time" to the Date() function in order to retrieve the datetime type.

  1. Utilizing Google Chrome as the browser.
  2. The Windows system is set to the EST timezone (-05:00).
  3. Daylight Saving Time (DST) begins on March 8th, 2020 at 2AM (EDT -04:00)

When entering the date and time as Date(2020,02,08,01), the output is displayed as "Sun Mar 08 2020 01:00:00 GMT-0500 (Eastern Standard Time)".

However, if the date and time specified includes the exact moment of the DST switch at 2AM, the result appears as "Sun Mar 08 2020 03:00:00 GMT-0400 (Eastern Daylight Time)".

This discrepancy results in the time being represented as "03" instead of "02".

I am seeking clarification on why the Date function does not behave as anticipated for this specific time period, as well as any solutions to resolve this issue.

Answer №1

We are observing DST on March 29th, and everything is functioning as expected in the GMT-1/GMT-2 time zone.

console.log(new Date(2020,2,29,0,0,0,0)); // The output in CEST is "2020-03-28T23:00:00.000Z"
console.log(new Date(2020,2,29,1,0,0,0))  // The output in CEST is "2020-03-29T00:00:00.000Z"
console.log(new Date(2020,2,29,2,0,0,0))  // The output in CEST is "2020-03-29T01:00:00.000Z"
console.log(new Date(2020,2,29,3,0,0,0))  // The output in CEDT is "2020-03-29T01:00:00.000Z" !!!
console.log(new Date(2020,2,29,4,0,0,0))  // The output in CEDT is "2020-03-29T02:00:00.000Z"

You might consider using UTC instead.

console.log(new Date(Date.UTC(2020,2,29,0,0,0,0))); // The output in CET is "2020-03-29T00:00:00.000Z"
console.log(new Date(Date.UTC(2020,2,29,1,0,0,0))); // The output in CET is "2020-03-29T01:00:00.000Z"
console.log(new Date(Date.UTC(2020,2,29,2,0,0,0))); // The output in CET is "2020-03-29T02:00:00.000Z"
console.log(new Date(Date.UTC(2020,2,29,3,0,0,0))); // The output in CET is "2020-03-29T03:00:00.000Z" 
console.log(new Date(Date.UTC(2020,2,29,4,0,0,0))); // The output in CET is "2020-03-29T04:00:00.000Z"

Alternatively, you could use UTC on the 8th:

console.log(new Date(Date.UTC(2020,2,8,0,0,0,0))); // The output in CET is "2020-03-08T00:00:00.000Z"
console.log(new Date(Date.UTC(2020,2,8,1,0,0,0))); // The output in CET is "2020-03-08T01:00:00.000Z"
console.log(new Date(Date.UTC(2020,2,8,2,0,0,0))); // The output in CET is "2020-03-08T02:00:00.000Z"
console.log(new Date(Date.UTC(2020,2,8,3,0,0,0))); // The output in CET is "2020-03-08T03:00:00.000Z" 
console.log(new Date(Date.UTC(2020,2,8,4,0,0,0))); // The output in CET is "2020-03-08T04:00:00.000Z"

Answer №2

The outcome is accurate.

When using Date(2020,02,08,01,59,00), it results in Sun Mar 08 2020 01:59:00 GMT-0500
Date(2020,02,08,02,00,00) will give you Sun Mar 08 2020 03:00:00 GMT-0400

During Spring, clocks move forward by an hour. You'll notice the GMT offset decreasing by one hour due to daylight savings time on the East Coast.

There is no 2AM (GMT-05) on March 8 in the Eastern timezone. After 01:59:59 GMT-05:00, the clock jumps directly to 03:00:00 GMT -04:00. This jump reflects the change in GMT, which acts as an offset.

Internally, Javascript stores the exact time in ticks from 1/1/1970 GMT00:00 when creating a new Date object. When retrieving time information like year and hour, your local timezone is applied to determine the correct local time. Thus, even though your date object reads Date(2020,02,08,02,00,00), JS still treats it as Standard (winter) time since there's no 2am in Daylight Savings time. The tick value remains the same as 03:00:00 GMT-00:40, with the hour being localized to your timezone. Essentially, 02:00:00 GMT-05:00 and 03:00:00 GMT-04:00 are indistinguishable but represent different timezones/daylight saving periods.

In Fall, your date/time parsing logic may face challenges as the hour between 1 and 2 occurs twice. According to TimeAndDate, on 11/1 at 2am, the clock shifts back to 1am. Therefore,

Date(2020, 10, 01, 01, 30, 00)

cannot definitively identify whether it's still EDT or already EST without specifying the current timezone/offset to UTC.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Display data in Bootstrap table using JQuery and JSON

Can anyone help me figure out how to populate my bootstrap table (in Database.Master) with data (in DatabaseUI.aspx.cs)? I need to dynamically add rows to the table using JQuery. Do I need to convert my string to JSON first? I believe I may have to add an ...

Unlimited rotation - using setInterval function

I am encountering an issue with removing the move class from my code. Can someone please check it out for me? let lis = document.querySelectorAll('li') let arr = []; for (let i = 0; i < lis.length; i++) { arr.push(lis[i]); } setInterval( ...

Can variables be transmitted through Real-Time Communication in JavaScript?

Currently, I am in the process of developing a multiplayer game using three.js. The basic framework is all set up and my next step is to implement the multiplayer aspect. After some research, I came across RTC as a solution that doesn't require comple ...

Trouble with basic JavaScript functionality in a React component

Here is a unique component code snippet: import React, {Component} from 'react'; import 'D:/School/Alta/interactiveweb/src/webapp/src/App.css' class Chat extends Component { test() { alert(); } render() { return <nav ...

Utilizing Electron API within an Angular Component

I'm hoping to utilize a locally stored video file in electron and play it within my angular component. Despite exposing the loadLocalFile function to prevent the need for setting nodeIntegration to true, I keep receiving a Security Warning : This re ...

How to apply dynamic styling to a MatHeaderCell using NgStyle?

My goal is to dynamically style a MatHeaderCell instance using the following code: [ngStyle]="styleHeaderCell(c)" Check out my demo here. After examining, I noticed that: styleHeaderCell(c) It receives the column and returns an object, however ...

Issue encountered while constructing my application (utilizing the "yarn run build" command and in Vercel)

Encountered an error during the build process, whether on the server or locally. This issue arises when using package managers like yarn, npm, and others. The error specifically points to a problem in the CSS file, but it only occurs in the production bu ...

I successfully managed to ensure that the splash screen is only displayed upon the initial page load. However, I am now encountering an issue where it fails to load again after I close the page. Is there any possible solution

After successfully implementing a splash screen that only plays once on page load, I encountered an issue. When I close the tab and revisit the page, the splash screen no longer plays. Is there a way to fix this problem? Perhaps by setting a time limit for ...

Defining types for functions that retrieve values with a specified default

My method aims to fetch a value asynchronously and return it, providing a default value if the value does not exist. async get(key: string, def_value?: any): Promise<any> { const v = await redisInstance.get(key); return v ? v : def_value; } W ...

Changing the background color of tabs in Angular

Being a beginner in HTML and CSS, I recently utilized the angular-tabs-component to create tabs. Now, I am looking to change the background color of the tab. Can someone guide me on how to achieve this? Below is the code snippet: HTML: <tabs (currentT ...

Creating an aperture in a form using a different shape in THREE.js

Currently, I am utilizing the D3-threeD2.js library to convert SVG files into THREE.Shape(s) that can be extruded with three.js. The process works smoothly, however, when it comes to incorporating holes, I encounter an issue. Imagine a shape resembling a ...

Using sinon.js version 1.10, jQuery version 2.1, and making synchronous requests

I have been attempting to simulate a server using sinon.js and calling it with jQuery.ajax, but I am facing issues getting it to work. Here is the code snippet: $(function() { var server = sinon.fakeServer.create(); server.respondWith('POST&apo ...

Exploring the process of defining a generic type for a function which accepts any Static Model and outputs instances of that Model using Sequelize

My task involves defining a function named FieldSearch with specific parameters: fieldSearch<SpecificModel extends Model>( model: ModelStatic<SpecificModel>, // Struggling with this part fields: Array< attributes of the static model p ...

How should res.render() and res.redirect() be properly utilized within Express framework?

I am struggling to understand the difference between res.render('viewname', {msg: 'Message' }) and res.redirect('route') The render function allows you to pass a "message", but the redirect function does not. However, ther ...

What is the best way to send events to connected sockets using socket.io directly from my Express 4 routes?

I have a question that others have asked before, but I'm struggling to benefit from their answers due to the unique Express setup I have. Currently, I have socket.io implemented and running smoothly on my server in a simple manner. Here is how it is ...

Rearrange the provided string in a particular manner

Looking to transform a string like '12:13:45.123 UTC Sun Oct 17 2021' into 'Sun Oct 17 2021 12:13:45.123 UTC' without calling slice twice. Is there a more elegant and efficient way to achieve this? Currently using: str.slice(18)+&apo ...

Unable to establish a websocket connection with either Amber or NPM, uncertain of the reason

Amber CLI (amberframework.org) - v0.11.3 Crystal 0.27.0 [c9d1eef8f] (2018-11-01) LLVM: 4.0.0 Default target: x86_64-unknown-linux-gnu npm 3.5.2 Attempting to incorporate sockets using Crystal Lang and Amber has hit a snag. Despite following the guidelines ...

How can I achieve a stylish scrolling header similar to a Google Blog for my website?

Google's blog features a unique header design - as you scroll down, the gray bar in the header moves up until it locks at the top of the screen. While CSS's position:fixed can achieve a similar effect, Google seems to have used a combination of p ...

Should medium-sized JavaScript arrays be compressed before sending them to the client via a socket?

I find myself considering whether it would be beneficial to use compression when sending medium sized arrays, containing small strings and numbers, from nodejs with socket.io to clients. The arrays are less than 1 MB in size. Would the time spent compressi ...

removing duplicate items from an array in Vue.js

I created a Pokemon App where users can add Pokemon to their 'pokedex'. The app takes a pokemon object from this.$store.state.pokemon and adds it to this.$store.state.pokedex. However, users can add the same pokemon multiple times to the pokedex, ...