Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

from Date I just want to subtract 1 day in javascript/angularjs

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)
like image 465
Codiee Avatar asked Aug 05 '14 10:08

Codiee


People also ask

How do I subtract a day in JavaScript?

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 a day 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

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);
like image 172
AndrewPolland Avatar answered Sep 19 '22 14:09

AndrewPolland