with the help of new Date() how i can achieve this. my code :
var temp =new Date("October 13, 2014 22:34:17");
console.log(new Date(temp-1));
requirement:
before: Aug 4, 2014 11:59pm (EDT)
after: Aug 3, 2014 11:59pm (EDT)
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.
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.
Subtract dates using getTime() method And that's how you can subtract dates in JavaScript.
let yesterday=new Date(new Date(). getTime() - (1 * 24 * 60 * 60 * 1000)); let last3days=new Date(new Date(). getTime() - (3 * 24 * 60 * 60 * 1000));
You need to specify what amount of time you are subtracting. At the moment its 1, but 1 what? Therefore, try getting the days using getDate()
and then subtract from that and then set the date with setDate()
.
E.g.
var temp = new Date("October 13, 2014 22:34:17");
temp.setDate(temp.getDate()-1);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With