I am wanting to display in a react native app a date given by the API.
The API is returning the following value: "2022-06-20T14:00:00.000"
I need to display to the user 2:00 PM
The problem is that when I try this:
const dateString = "2022-06-20T14:00:00.000"
const date = new Date(dateString);
console.log(date)
The date has been changed to: 2022-06-20T14:00:00.000Z
Meaning it is 2pm UTC time when it should be 2pm local time.
CodePudding user response:
On the end of the new Date variable, if you add the .toLocaleTimeString(), it should then output as 2:00:00PM.
const date = new Date(dateString).toLocaleTimeString();
CodePudding user response:
you can use moment library with its localized formats, here is the documentation for more of its formats https://momentjscom.readthedocs.io/en/latest/moment/04-displaying/01-format/
const dateString = "2022-06-20T14:00:00.000"
console.log(moment(dateString).format('LT'))