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

how to convert ISO date time to YYYYMMDDHHmmss string format?

I have a date time in string in this ISO format:

2023-08-07T09:38:43+07:00

but now I need to convert it to YYYYMMDDHHmmss format, so I expect I will get this string

20230807093843

how to do that using Javascript without using Moment or another libarary?

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

>Solution :

Here this is one way of doing it I guess.

   
const originalDate = "2023-08-07T09:38:43+07:00";

// Parse the original date string
const parsedDate = new Date(originalDate);

// Extract individual components
const year = parsedDate.getFullYear();
const month = (parsedDate.getMonth() + 1).toString().padStart(2, '0');
const day = parsedDate.getDate().toString().padStart(2, '0');
const hours = parsedDate.getHours().toString().padStart(2, '0');
const minutes = parsedDate.getMinutes().toString().padStart(2, '0');
const seconds = parsedDate.getSeconds().toString().padStart(2, '0');

// Create the final formatted date string
const formattedDate = `${year}${month}${day}${hours}${minutes}${seconds}`;

console.log(formattedDate); // Output: 20230807093843
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