-6

i want to get my country date like this 2020-03-11T02:59 i try to JSON and toISOString but it give me the hours does not true

1 Answers1

-2

You can change the timezone with the offset at the end of the date string (e.g. -05:00).

And then create the date string you want using get functions.

You will need to pad the month, day, hour and minutes. However, the .get (eg .getMonth) functions return numbers, so you will also have to convert them to strings.

For example: event.getMonth().toString().padStart(2,0);

event is your date object.

.getMonth() returns the numeric value of the month of your date object

.toString() converts that number to a string value

.padStart(2,0) will add zeros to the front of the string if it is less than 2 characters.

// Set the date with timezone offset
let event = new Date("2020-03-11T02:59:00-08:00");

// Format your string
let newEvent = `${event.getFullYear()}-${event.getMonth().toString().padStart(2,0)}-${event.getDate().toString().padStart(2,0)}T${event.getHours().toString().padStart(2,0)}:${event.getMinutes().toString().padStart(2,0)}`;

console.log(newEvent);
Jeff Vdovjak
  • 1,833
  • 16
  • 21