Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to subtract days from a plain Date?

Is there an easy way of taking a olain JavaScript Date (e.g. today) and going back X days?

So, for example, if I want to calculate the date 5 days before today.

like image 607
jonhobbs Avatar asked Aug 18 '09 20:08

jonhobbs


People also ask

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.

How do I subtract days from a date in typescript?

Show activity on this post. let yesterday=new Date(new Date(). getTime() - (1 * 24 * 60 * 60 * 1000)); let last3days=new Date(new Date(). getTime() - (3 * 24 * 60 * 60 * 1000));

How do you subtract days in Python?

You can subtract a day from a python date using the timedelta object. You need to create a timedelta object with the amount of time you want to subtract. Then subtract it from the date.


1 Answers

Try something like this:

 var d = new Date();  d.setDate(d.getDate()-5); 

Note that this modifies the date object and returns the time value of the updated date.

var d = new Date();    document.write('Today is: ' + d.toLocaleString());    d.setDate(d.getDate() - 5);    document.write('<br>5 days ago was: ' + d.toLocaleString());
like image 92
Stephen Wrighton Avatar answered Sep 21 '22 13:09

Stephen Wrighton