Reputation: 5687
How can i get the previous year
start date/month
and end date/month
Below is the code, i have tried, but its not working...
var lastyear = new Date(new Date().getFullYear(), 0, 1);
lastyear.setFullYear(lastyear.getFullYear() - 1);
var start = (new Date(lastyear, 0, 1)).getTime(),
end = (new Date(lastyear, 11, 31)).getTime();
Technically i want 01/01/2015
to 12/31/2015
. What is the mistake i am doing here?
Upvotes: 1
Views: 2663
Reputation: 123
You ran into issues because the you were using the Date() constructor incorrectly. According to http://www.w3schools.com/js/js_dates.asp, the Date constructor accepts the following inputs:
new Date(year, month, day, hours, minutes, seconds, milliseconds)
In your original code, you were passing a Date object into the "year" argument. I changed:
new Date(lastyear, 0, 1)
to
new Date(lastyear.getFullYear(), 0, 1)
which fixed the problem.
var lastyear = new Date(new Date().getFullYear() - 1, 0, 1);
var start = (new Date(lastyear.getFullYear(), 0, 1)).getTime(),
end = (new Date(lastyear.getFullYear(), 11, 31)).getTime();
Is this the solution you are looking for?
Upvotes: 5