skip to Main Content

I am working with angular project and need to send correct date and time to the server.

First, I have taken the offset by using below code

let offset=new Date().getTimezoneOffset();
let datestring="2024-12-21T00:00:00"
let newdate=new Date(datestring);

The ‘newdate’ display in the console like below
Dec 21 2024 00:00:00 GMT-0500 (Eastern Standard Time)

But the offset value is 240 that means four hours.

As you can see offset value is not matching for the standard time offset value which is showing -0500. Can you please let me know why offset value taken from getTimezoneOffset() method is different from the offset value in the newdate object (-0500)?. There is an one hour difference.

Thanks in advance

2

Answers


  1. The difference in the timezone offset comes probably from the different dates you used. At the time of posting, we have 2024-09-10 (the exact day is not that relevant) which means, in your timezone the DST (daylight savings time) is active.

    On the date you entered for comparison (2024-12-21) the DST will not be active. This results in an offset of one hour between DST and non-DST – exactly what you have observed.

    Login or Signup to reply.
  2. A Date always will try to use the local time if you don’t give a timezone reference.
    When you create a manual date, if you want to refer to the standard time, you should add a Z in the end

    let datestring="2024-12-21T00:00:00Z"
    

    The same for database stored dates, if they are not using the timezone rules, it will use the local time.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search