Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding date by subtracting X number of days from a particular date in Javascript

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 and the other is the number of days that needs to be subtracted.

For example, I pass my argument date as 27 July 2009 and i pass my other argument as 3. So i want to calculate the date 3 days before 27 July 2009. So the resultant date that we should get is 24 July 2009. How is this possible in JavaScript. Thanks for any help.

like image 644
ajithmanmu Avatar asked Jul 27 '09 12:07

ajithmanmu


People also ask

How do you minus days from date in JS?

To subtract days to a JavaScript Date object, use the setDate() method. Under that, get the current days and subtract days. JavaScript date setDate() method sets the day of the month for a specified date according to local time.

How do you subtract days from a date?

Therefore, you can add or subtract days as easy as adding or minus the number of days in Excel. 1. Select a blank cell you will place the calculating result, type the formula =A2+10, and press the Enter key. Note: For subtracting 10 days from the date, please use this formula =A2–10.

Can you subtract dates in JavaScript?

Subtract dates using getTime() method And that's how you can subtract dates in JavaScript.

How do I subtract days from a date in typescript?

let yesterday=new Date(new Date(). getTime() - (1 * 24 * 60 * 60 * 1000)); let last3days=new Date(new Date(). getTime() - (3 * 24 * 60 * 60 * 1000));


1 Answers

Simply:

yourDate.setDate(yourDate.getDate() - daysToSubtract); 
like image 93
Daniel F. Thornton Avatar answered Oct 05 '22 13:10

Daniel F. Thornton