I have been facing an issue with a JavaScript function that is supposed to adjust the date when a button is clicked. The function is intended to utilize the getDate() function to subtract 7 days from the current date value displayed in the textbox. However, despite my attempts to troubleshoot and correct the problem, I have not been successful in resolving it. The main issue is that every time I click the button, instead of deducting another seven days from the date, it jumps to an entirely different date. For instance, if the date in the textbox is 05/01/2014, clicking the button takes it to 29/12/2014, and clicking it again shifts it to 28/11/2015, rather than the expected 22/12/2014.
Below is the excerpt of my JavaScript code:
var fromDateIn = new Date(formatDate(document.getElementById('<%= txtFromDate.ClientID%>').value));
var newdate = new Date(fromDateIn);
newdate.setDate(newdate.getDate() - 7);
var nd = new Date(newdate);
document.getElementById('<%= txtFromDate.ClientID%>').value = formatDate(nd);
function formatDate(inputDate) {
var d = new Date(inputDate || Date.now()),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [day, month, year].join('/');
}
I am seeking assistance in identifying the correct method to consistently subtract 7 days from the current date value in the textbox. Any insights are greatly appreciated.
Thank you