I am using a library to calculate the age from date of birth. I am taking date of birth as an input which is in the format of dd/mm/yy but the library that calculated the age accepts it in the format of mm/dd/yy. One solution to this is to change the date selector format in the application but I dont want to do that since it gets confusing.
I searched the solution on stackoverflow but couldnt find the solution here- How to format a JavaScript date
CodePudding user response:
How about a simple split and join:
var yourDate = "10/12/2021";
var arrayOfDate = yourDate.split("/");
console.log([arrayOfDate[1], arrayOfDate[0], arrayOfDate[2]].join('/'));
CodePudding user response:
Just split
it with /
and then use destructure
it and get the desired result as:
const result = `${mm}/${dd}/${yyyy}`;
var oldDate = "10/12/2021";
var [dd, mm, yyyy] = oldDate.split("/");
const newDate = `${mm}/${dd}/${yyyy}`;
console.log(newDate);