I am very new to JavaScript and it took me some time to come up with the following function in order to get current date and subtract a year from it:
function getDate() {
var now = new Date();
// get current timezone specific date/time
// timezoneoffset value converts returned offset value to milliseconds
// get current date value - 1 year
var dateMinusOneYear = new Date(now.getTime() - 31556952000) + (now.getTimezoneOffset() * 60000);
var dateStr =
("0000" + (dateMinusOneYear.getFullYear())).slice(-4) + "-" +
("00" + (dateMinusOneYear.getMonth() + 1)).slice(-2) + "-" +
("00" + dateMinusOneYear.getDate()).slice(-2);
return (dateStr);
}
When I invoke the function, I get the following error: TypeError: dateMinusOneYear.getFullYear is not a function
Can anyone please advise? Some might advise to not use the new Date method at all due to problematic reasons, but I have to use it specifically for some unrelated reasons.
This makes no sense at all
new Date(now.getTime() - 31556952000) + (now.getTimezoneOffset() * 60000);
Maybe you were trying to accomplish this?
new Date(now.getTime() - 31556952000 + now.getTimezoneOffset() * 60000);