1

I'm Trying to convert the below string to date in the format(dd-mm-yyyy)

let dob = /Date(1646764200000+0530)/

The above value of Date is given in string, How to convert it to date using JavaScript, Tried a lot of methods, but it shows an error as invalid date.?

RobG
  • 142,382
  • 31
  • 172
  • 209
AjaiJA
  • 67
  • 1
  • 8

1 Answers1

2

Converting to the DD-MM-YYYY requires formatting.

Here is one way to extract the date portion using the slice() method then formatting the output to your desired format. This assumes that the input date is always in the same form and length.

let dob = /Date(1646764200000+0530)/;

let date = new Date(+(""+dob).slice(6,19)).toLocaleString('en-GB',{year:"numeric",day:"2-digit",month:"2-digit"}).split("/").join("-");

console.log(date)
Mohsen Alyafei
  • 4,765
  • 3
  • 30
  • 42