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
Asked
Active
Viewed 204 times
-6

alaa kaheel
- 1
- 2
-
do you mean your local time? – Daniel A. White Mar 11 '20 at 02:08
-
yes my localtime – alaa kaheel Mar 11 '20 at 02:14
-
i'm not sure what you mean about the hours. `toISOString` should be enough... – Daniel A. White Mar 11 '20 at 02:21
-
1@DanielA.White I believe he is saying that it's not giving him his local time zone, but that he is getting UTC. This answer may help: https://stackoverflow.com/questions/10087819/convert-date-to-another-timezone-in-javascript – Jeff Vdovjak Mar 11 '20 at 02:23
1 Answers
-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