Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

covnert european time format to US timeformat withAM/PM

I have a string I am trying to display in EST time with AM / PM additions. Somehow it is not working the way I want it to.

I tried this:

let item = "2022-02-16T20:08:29"

return item.toLocaleString("en-US", {
    timeZone: "EST"
  });

expected output 2/16/2022, 8:08:29 PM

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

output I am getting 2022-02-16T20:08:29

>Solution :

The time you are converting is still a string, to access the method toLocaleString you need to be accessing the Date Object.

The date object accepts a string to parse in the constructor as follows.
If you need to convert timezone, then you will need to add in the timezone parameter.

let item = '2022-02-16T20:08:29';

function formatDate(date_string){
  let date = new Date(date_string);
  return date.toLocaleString("en-US");
}

console.log(formatDate(item))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading