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 can I convert a "YMDhm" date string to "Y-M-D h:m" format?

My number is :

"202112110836"

I want separate this string into to:

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

"2021-12-11 08:36"

>Solution :

I don’t think such a function exists in jQuery or JavaScript itself for that specific date format; the other solution is to simply fetch each subsection of the string and split it accordingly to the format you want.

const input = "202112110836";

// Fetch each specific date value by substring.
const year = input.substring(0, 4);
const month = input.substring(4, 6);
const day = input.substring(6, 8);
const hour = input.substring(8, 10);
const minute = input.substring(10, 12);

// Combine it all together.
const output = year + "-" + month + "-" + day + " " + hour + ":" + minute;
console.log(output);
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