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 get only date without time from a string contain date and time?

I’ve a date like that 01/03/22 22:09 and I want to store it in DB just in a format of date like like that '2022-03-01'; I’ve tried Regex but still get time in an hour:minute too
How can I delete it?

const date_time = '22/01/03 22:09';
const only_date = date_time.replace(/(\d{2})\/(\d{2})\/(\d{2})/,"'20$3-$2-$1'");
//console.log(only_date.substring(0,only_date.lastIndexOf("'")+1))
console.log(only_date)

PS: without using substring or slice… just with regex is there a way to do that?

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 :

You missed the .*

const date_time = '22/01/03 22:09';
const only_date = date_time.replace(/(\d{2})\/(\d{2})\/(\d{2}).*/,"'20$3-$2-$1'");
console.log(only_date)
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