Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to find date by subtracting X number of days from a particular date in JavaScript. My JavaScript function accepts 2 parameters. One is the date value (yyyy-mm- dd) and the other is the number of days that needs to be subtracted.

I have subtract 30days consecutively for a year.

eg: 2014/12/30
2014/11/30
2014/10/30
2014/09/30
.....
.
.
.
.
.
2014/01/30


I have tried like this :

function statement_cycle(date,days)
{
return new Date(
date.getFullYear(),
date.getMonth(),
date.getDate() – days,
date.getHours(),
date.getMinutes(),
date.getSeconds(),
date .getMilliseconds()
);
}

i have calculated for the one month. i need to calculate for the remaining 11 months
Posted
Updated 2-Nov-14 21:40pm
v2
Comments
[no name] 3-Nov-14 2:26am    
What is the issue ?
Maciej Los 3-Nov-14 3:05am    
What have you done till now? Where are you stuck?
Maciej Los 3-Nov-14 3:38am    
Use "Improve question" widget and post above code there.
Sinisa Hajnal 3-Nov-14 4:37am    
What are you trying to do? If you need last day of the month (or current day in all months) you could generate the dates without subtraction.
priya dharshan 3-Nov-14 4:50am    
based on the date from json text, i need to calculate the date (subtract the date from x days)for 12 consecutive months.

1 solution

Try this. 0 as day parameter will return last month day.

JavaScript
for (month=1; month <= 12; month++) {
    var lastDay = new Date(date.getFullYear(), month, 0);

    // do something with your last day
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900