Skip to content
Advertisement

Convert date string to a date object

Getting the date from the user and storing the value as a string variable. The value looks like this Fri Aug 27 2021 00:00:00 GMT+0530 (India Standard Time)

I want to convert that string again into a new Date() object for further configurations (I need to add the number of days to the selected date and show it on another Angular mat-datepicker) How can I do this..?

Advertisement

Answer

You can cast a date string directly to a Date object like:

let myDate = new Date("Fri Aug 27 2021 00:00:00 GMT+0530 (India Standard Time)");

You can manipulate your date as follows:

// For instance adding a day:
let today = new Date("Fri Aug 27 2021 00:00:00 GMT+0530 (India Standard Time)");

let tomorrow = new Date()
tomorrow.setDate(today.getDate() + 1);

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement