[SOLVED] Convert string(Date(1646764200000+0530)) to date in the format(dd-mm-yyyy) using javascript

Issue

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.?

Solution

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)

Answered By – Mohsen Alyafei

Answer Checked By – Timothy Miller (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *