Why is javascript date by an hour behind?

Hi, I am trying to create a unix timestamp to represent the last time of the current day (23:59:59) as follows:
current_date = new Date(); end = new Date(current_date.getFullYear(), current_date.getMonth(), current_date.getDate(),23,59,59); end = end.getTime() / 1000; 

When I warn the unix timestamp and convert it back to datetime, although it is an hour behind and represents 22:59:59, not 23:59:59.

I need to pass 24 to the date function for the hour parameter instead of 23, if I want 11 pm, is that right?

I am in England, so my time should be in GMT

+4
source share
1 answer

new Date() will create a date in your time zone, and timestamps in UTC. It seems that you are in BST (GMT + 1), therefore, this is an error at a time.

Create a date instead, and then use setUTCHours(23) , setUTCMinutes(59) and setUTCSeconds(59) to get the correct timestamp.

+5
source

Source: https://habr.com/ru/post/1437422/


All Articles