0

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

  • You should search [\[javascript\] reformat date](https://stackoverflow.com/search?q=%5Bjavascript%5D+reformat+date). :-) There is always `'31/12/2021'.replace(/(\d+)\/(\d+)\/(\d+)/g, '$2/$1/$3')`. – RobG Dec 10 '21 at 09:33

2 Answers2

1

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('/'));
Deepak
  • 2,660
  • 2
  • 8
  • 23
0

Just split it with / and then use destructure it and get the desired result as:

const result = `${mm}/${dd}/${yy}`;

var oldDate = "10/12/21";
var [dd, mm, yy] = oldDate.split("/");
const newDate = `${mm}/${dd}/${yy}`;
console.log(newDate);
DecPK
  • 24,537
  • 6
  • 26
  • 42