I have the date as follows,
let date = '2024-03-11';
I want to add plus 1 day to above date
let newDate = mew Date(date); -> converting to local date -> 2024-03-10 (becoming -1)
newDate.setDate(newDate.getDate() + 1);
It worked well but the problem is I am getting the local date when I am using new Date() method which is converting my date to 2024-03-10
Can some one help me how to add 1 date to UTC date without getting it converted to local time zone.
3
Answers
To add one day to a UTC date without converting it to the local time zone, you can use the
Date.UTC()
method along with theDate
object constructor. Here’s how you can do it:This code creates a new
Date
object with UTC time and adds one day to it without converting it to the local time zone.To add 1 day to a UTC date without it getting converted to the local timezone, you can directly manipulate the date string or use the Date.UTC method to ensure the operation is performed in UTC. Here’s a method that modifies the date string and creates a new date object in UTC:
for nodejs, you can use moment: