Home > Back-end >  How can get only date without time from a string contain date and time?
How can get only date without time from a string contain date and time?

Time:01-05

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?

CodePudding user response:

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)

CodePudding user response:

try this:

date_time.replace(/(\d{2})\/(\d{2})\/(\d{2}).<b>*</b>/,"'20$3-$2-$1'");
  •  Tags:  
  • Related