I'm currently working on a JavaScript function that extracts a date from a string and then parses it to set the start and end dates for a date filter. However, I'm encountering an issue with the date parsing part.
$("#startDateF").data("initialValue", startDateString);
$("#endDateF").data("initialValue", endDateString);
These lines are getting used in a reset button function which will retrieve these values and set the current values of the date filters back to the initialValues.
Here is the code:
var cDate = dText.match(/\d{2}-\d{2}-\d{4}/)[0];
console.log(cDate);
let initialStartDate = new Date(cDate);
let initialEndDate = new Date(initialStartDate.getFullYear(), initialStartDate.getMonth() + 1, initialStartDate.getDate());
startDateString = formatDate(initialStartDate);
endDateString = formatDate(initialEndDate);
$("#startDateF").data("initialValue", startDateString);
$("#endDateF").data("initialValue", endDateString);
function formatDate(date) {
let day = ("0" + date.getDate()).slice(-2);
let month = ("0" + (date.getMonth() + 1)).slice(-2);
let year = date.getFullYear();
return day + "-" + month + "-" + year;
}
I tried using the formatDate function to format the date, but it seems it is not working correctly. When I used this method it is setting startDate and endDate to aN-aN-NaN. I am expecting that the startDate is set to cDate and endDate is set to the same date, but the in the next month. E.g., cDate = 16-04-2023 startDate = 16-04-2023 endDate = 16-05-2023