How to subtract days from a plain Date?
Asked Answered
N

38

928

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.

Nietzsche answered 18/8, 2009 at 20:37 Comment(0)
C
1443

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());
Colony answered 18/8, 2009 at 20:41 Comment(21)
The output of that gives me a long string of integers. Any idea of how to format it into 05/14/2012?Globose
Given that this is at the top of google, I figure I'd answer the above comment: new Date(new Date().setDate(new Date().getDate()-5)) - that will be 5 days ago. In the example of the answer, pass it to a new date to get a date object. So new Date(d) is what you want.Pampuch
That 'long string of integers' is the UTC time in milliseconds past the epoch. If that concept is new to you, or you don't realize why it's important to use that format, I'd recommend reading the introduction section here (you can skip everything after): tablespace.net/papers/unix_time.htmlPandorapandour
Problem with this is that you can end up with 00 which is not a valid value for date.Debility
@Debility What browser are you using? I just tried this in the Chrome console and it worked fine.Baloney
Actually it does seem to work as expected in chrome, not sure why it didn't before, I must have done something differently then. Maybe if you did not use setDate and just wanted the day you could get 00.Debility
setDate(-1) will set date to last day of the monthAloisia
@Globose asked how to format the number, not what the number is. There's no need for condescending answers. To format the number, use one of the Date toString methods like toISOString, toDateString, etc.Centum
This is not the correct answer. As stated above this doesn't work fine if d is the first of a month.Slingshot
@Aloisia When sending a simple interget number to .setDate(), the returned value represents the day value of the current month (in the set date value). For example, if the date value of the variable is "2017-02-17" and I run .setDate on it with the value of 5, the returned date value will be "2017-02-05". - Added "5" to a date value that holds the month of February 2017, I get the value of the 5th of February 2017.Tangential
@Globose formatted string (since raw js doesn't have custom formatting) ` var now = new Date(); dateToString = ('0' + (now.getMonth() + 1).toString()).slice(-2) + '/' + ('0' + now.getDate().toString()).slice(-2) + '/' + now.getFullYear().toString().slice(-2) ;`Microspore
@Tangential setDate() is more complicated than that. It will set the date in the current month, but it will also apply bounding logic. If you call setDate(0) it will decrement the month and set date to the last of that month. If you call setDate(30) on a date in February, it will return March 2nd. That's why the getDate() - 1 solution works if you're passing it to setDate but not if you're just looking for the integer date of yesterday. In that case, you should use d.setDate(d.getDate() - 1).getDate() to normalize it against the calendar.Rosalba
@Aloisia setDate(-1) will set to the second last day of the previous month. For the last day you want setDate(0) (still returns the previous month, but you get the idea).Snide
seems to work.. even for the first day of the month, as seen here in Chrome console: > var d = new Date() > d > Fri Nov 01 2019 15:13:54 GMT+0100 (Mitteleuropäische Normalzeit) > d.setDate(d.getDate() - 1) > 1572531234550 > d > Thu Oct 31 2019 15:13:54 GMT+0100 (Mitteleuropäische Normalzeit)Bashibazouk
setDate(-1) does not work on leap years, instead of expected 29 you get 28Abhor
No-one seems to read comments, but let me summarize: #1 setDate(0), IS VALID, it gives you the last day of the prior month. #2 THIS WORKS FOR LEAP YEARS let d = new Date(1996,2,1); d.setDate(0); d gives you Thu Feb 29. And I'll add: #3 setDate function returns time as a number, but the VARIABLE has been mutated and IS STILL IN DATE FORM.Protect
@Debility - please show your work. I'm not following what you mean or how you got to that conclusion. Thanks.Passivism
@ChrisH First of all I did not post the answer, and secondly this question was asked and answered 12 years ago.Debility
Ah, sorry. Must have tagged the wrong person. Apologies.Passivism
@DominicM—can you please delete your comment regarding "Problem with this is that you can end up with 00 which is not a valid value for date" It isn't a problem, the date will be set to the last day of the previous month, e.g. 5 Jan - 5 days gives 0 Jan which becomes 31 Dec. Why it's received 9 votes is a mystery.Nymphomania
@Augustas— setDate(-1) sets the date to the second last day, not the last per the comment by @Molomby. So in a leap year setting March to -1 should return 28 February and for non–leap years, 27 February. :-)Nymphomania
B
139
var dateOffset = (24*60*60*1000) * 5; //5 days
var myDate = new Date();
myDate.setTime(myDate.getTime() - dateOffset);

If you're performing lots of headachy date manipulation throughout your web application, DateJS will make your life much easier:

http://simonwillison.net/2007/Dec/3/datejs/

Bassarisk answered 18/8, 2009 at 20:43 Comment(7)
it's an edge case, but this can fail around the start/end of daylight saving time. you're not subtracting 5 days, but 5*24 hours. the first day of daylight saving time is only 23 hours long, and the last is 25 hours long. it usually doesn't matter, but it's something to consider.Central
@Central - Interesting, I missed that nuance entirely.Bassarisk
@Central I hate daylight savings time... and timezones. We should all move to GMT.Extrude
@Extrude not to burst your bubble, but they use DST even in Greenwich. So if you really want to be on GMT, you're restricted to Iceland and a few west-African countries.Central
@Central Well, that's darn inconvenient.Extrude
The problem with this method, is that it doesn't work with new year etc.Henrique
@danielWilliams How does this not work with a new year? getTime() and setTime() are milliseconds since the epoch. It shouldn't matter if the time crosses a year boundary.Postwar
T
70

It goes something like this:

var d = new Date(); // today!
var x = 5; // go back 5 days!
d.setDate(d.getDate() - x);
Timer answered 18/8, 2009 at 20:41 Comment(4)
The output of that gives me a long string of integers. Any idea of how to format it into 05/14/2012?Globose
The return value of d.setDate is indeed an integer value. However, you are probably not actually interested in the return value at this point, since the actual value of "d" has been modified. Your real question is now how to format your date, which is now in "d" and not the return value of setDate at all. (Well, it actually is the return value, but it is not a date by that point and I do not wish to confuse you--just use d, it will be quicker). For formatting dates, you want the d.getMonth(), d.getFullYear(), and d.getDate() methods. You want to add one to d.getMonth(), since Jan is 0.Timer
As a simple method: function getDaysAgo(b){var a=new Date;a.setDate(a.getDate()-b);return a}; then just var daysAgo45 = getDaysAgo(45);Octangle
@MiroKrsjak Of course this works if you cross the month end/beginning. Why wouldn't it work?Timer
T
50

If you want it all on one line instead.

5 days from today

//past
var fiveDaysAgo = new Date(new Date().setDate(new Date().getDate() - 5));
//future
var fiveDaysInTheFuture = new Date(new Date().setDate(new Date().getDate() + 5));

5 days from a specific date

 var pastDate = new Date('2019-12-12T00:00:00');

 //past
 var fiveDaysAgo = new Date(new Date().setDate(pastDate.getDate() - 5));
 //future
 var fiveDaysInTheFuture = new Date(new Date().setDate(pastDate.getDate() + 5));

I wrote a function you can use.

function AddOrSubractDays(startingDate, number, add) {
  if (add) {
    return new Date(new Date().setDate(startingDate.getDate() + number));
  } else {
    return new Date(new Date().setDate(startingDate.getDate() - number));
  }
}

console.log('Today : ' + new Date());
console.log('Future : ' + AddOrSubractDays(new Date(), 5, true));
console.log('Past : ' + AddOrSubractDays(new Date(), 5, false));
Tisbee answered 12/11, 2019 at 23:36 Comment(4)
Welcome to Stack Overflow _ Although you have given a good coded answer the comment post at the top (yellow background) specifically states that "that provide some explanation and context" are required & "explain why your answer is right, ideally with citations" _ Please edit your answer with the above requirementsReflection
Shouldn't it be 'fiveDaysAgo' and 'fiveDaysIntheFuture' in the first 3 lines in your code snippet?Pilsudski
@MartinKomischke you are right. I must have missed that during my copy paste from where I was actually using it for work. Good catch!Tisbee
You don't need the add parameter, if you want to subtract just pass -5 and that's it :)Gurango
S
39

I noticed that the getDays+ X doesn't work over day/month boundaries. Using getTime works as long as your date is not before 1970.

var todayDate = new Date(), weekDate = new Date();
weekDate.setTime(todayDate.getTime()-(7*24*3600000));
Shiprigged answered 23/6, 2014 at 16:3 Comment(5)
I noticed the same. I'm surprised more haven't, or am I missing something?Rodrigorodrigue
This is incorrect, new Date().setDate(-1) will give you the last day of the previous month.Neron
This is also incorrect around the edge case of start/end of daylight saving time. Your approach is subtracting 7*24 hours, under the assumption that every day has 24 hours. The first day of DST is only 23 hours long, and the last day is 25 hours long. setDate(getDate()-7) does not have this problem.Central
getTime works off universal time. So will correctly subtract 7 days time from the Date object. When you convert it to a string in your specified time zone it will correctly show the time for that zone.Shiprigged
Be careful on the time change day. When we fall back in fall, that day has 25 hours, so subtracting 24 hours from midnight will land you in the same day. But this technique (subtracting milliseconds) WILL work if you round off the day after every operation to the nearest midnight. You can do that by first adding 12 hours, then setting the hours to zero.Protozoon
A
31

I find a problem with the getDate()/setDate() method is that it too easily turns everything into milliseconds, and the syntax is sometimes hard for me to follow.

Instead I like to work off the fact that 1 day = 86,400,000 milliseconds.

So, for your particular question:

today = new Date()
days = 86400000 //number of milliseconds in a day
fiveDaysAgo = new Date(today - (5*days))

Works like a charm.

I use this method all the time for doing rolling 30/60/365 day calculations.

You can easily extrapolate this to create units of time for months, years, etc.

Anatol answered 1/12, 2016 at 16:35 Comment(2)
If using TypeScript this needs to be new Date(today.getTime() - (5*days))Centesimal
This may give an incorrect result if the code is run near 2am and daylight savings time has changed within the last 5 days.Camarilla
T
29

get moment.js. All the cool kids use it. It has more formatting options, etc. Where

var n = 5;
var dateMnsFive = moment(<your date>).subtract(n , 'day');

Optional! Convert to JS Date obj for Angular binding.

var date = new Date(dateMnsFive.toISOString());

Optional! Format

var date = dateMnsFive.format("YYYY-MM-DD");
Tableware answered 25/2, 2016 at 14:22 Comment(2)
by passing your date into moment, you will start getting warnings. Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are discouraged and will be removed in an upcoming major release. Please refer to momentjs.com/guides/#/warnings/js-date for more info.Porridge
"All the cool kids use it" was once true, but not anymore. momentjs.com/docs/#/-project-statusLoess
C
19

A few of the existing solutions were close, but not quite exactly what I wanted. This function works with both positive or negative values and handles boundary cases.

function addDays(date, days) {
    return new Date(
        date.getFullYear(),
        date.getMonth(),
        date.getDate() + days,
        date.getHours(),
        date.getMinutes(),
        date.getSeconds(),
        date.getMilliseconds()
    );
}
Cushiony answered 17/5, 2015 at 0:26 Comment(2)
does it 'spill' over to next month if I use this method to add 20 days to 15th of current month? It does not seem to.Feaster
@Feaster - yes, it correctly increments the month if necessary. For example, adding 20 days to Aug 15: addDays(new Date(2015, 07, 15), 20) will return "Fri Sep 04 2015 00:00:00 GMT-0700 (PDT)"Cushiony
B
17

Without using the second variable, you can replace 7 for with your back x days:

let d=new Date(new Date().getTime() - (7 * 24 * 60 * 60 * 1000))
Brine answered 30/11, 2017 at 17:50 Comment(1)
I commented this on another entry, but it holds true here too: due to time adjustments not all days are 86.4m milliseconds. This could distort the result by +/- 1 dayProtect
S
15

I made this prototype for Date so that I could pass negative values to subtract days and positive values to add days.

if(!Date.prototype.adjustDate){
    Date.prototype.adjustDate = function(days){
        var date;

        days = days || 0;

        if(days === 0){
            date = new Date( this.getTime() );
        } else if(days > 0) {
            date = new Date( this.getTime() );

            date.setDate(date.getDate() + days);
        } else {
            date = new Date(
                this.getFullYear(),
                this.getMonth(),
                this.getDate() - Math.abs(days),
                this.getHours(),
                this.getMinutes(),
                this.getSeconds(),
                this.getMilliseconds()
            );
        }

        this.setTime(date.getTime());

        return this;
    };
}

So, to use it i can simply write:

var date_subtract = new Date().adjustDate(-4),
    date_add = new Date().adjustDate(4);
Steal answered 3/9, 2013 at 15:38 Comment(1)
You can use d.setDate(d.getDate() + days) with both positive and negative values for days to add and subtract days respectively. And it should work on the instance (as other Date methods do), not create and return a copy.Nymphomania
B
14

I like doing the maths in milliseconds. So use Date.now()

var newDate = Date.now() + -5*24*3600*1000; // date 5 days ago in milliseconds

and if you like it formatted

new Date(newDate).toString(); // or .toUTCString or .toISOString ...

NOTE: Date.now() doesn't work in older browsers (eg IE8 I think). Polyfill here.

UPDATE June 2015

@socketpair pointed out my sloppiness. As s/he says "Some day in year have 23 hours, and some 25 due to timezone rules".

To expand on that, the answer above will have daylightsaving inaccuracies in the case where you want to calculate the LOCAL day 5 days ago in a timezone with daylightsaving changes and you

  • assume (wrongly) that Date.now() gives you the current LOCAL now time, or
  • use .toString() which returns the local date and therefore is incompatible with the Date.now() base date in UTC.

However, it works if you're doing your math all in UTC, eg

A. You want the UTC date 5 days ago from NOW (UTC)

var newDate = Date.now() + -5*24*3600*1000; // date 5 days ago in milliseconds UTC
new Date(newDate).toUTCString(); // or .toISOString(), BUT NOT toString

B. You start with a UTC base date other than "now", using Date.UTC()

newDate = new Date(Date.UTC(2015, 3, 1)).getTime() + -5*24*3600000;
new Date(newDate).toUTCString(); // or .toISOString BUT NOT toString
Barely answered 16/4, 2015 at 11:33 Comment(2)
This is not precise. Some day in year have 23 hours, and some 25 due to timezone rules.Quinquereme
I would worry about failure conditions when there are leap seconds... so perhaps not good for critical code.Energy
P
13

split your date into parts, then return a new Date with the adjusted values

function DateAdd(date, type, amount){
    var y = date.getFullYear(),
        m = date.getMonth(),
        d = date.getDate();
    if(type === 'y'){
        y += amount;
    };
    if(type === 'm'){
        m += amount;
    };
    if(type === 'd'){
        d += amount;
    };
    return new Date(y, m, d);
}

Remember that the months are zero based, but the days are not. ie new Date(2009, 1, 1) == 01 February 2009, new Date(2009, 1, 0) == 31 January 2009;

Privily answered 18/8, 2009 at 20:44 Comment(4)
What happens if you add for example 50 days to a date this way? Will it set the date to 68 August 2009? Or are you sure that this always wraps over to the appropriate month and/or year correctly?Lippizaner
Used this to add addDays, addMonths and addYears to Date.prototype. Nice and simple.Zagreus
This should be the correct answer - it works even on the last day of the year when the accepted answer fails.Phraseology
Doesn't work for dates with years from 1 to 99. ;-) Much better to just use the get* and set* methods.Nymphomania
A
9

Some people suggested using moment.js to make your life easier when handling dates in js. Time has passed since those answers and it is noteworthy, that the authors of moment.js now discourage its use. Mainly due to its size and lack of tree-shaking-support.

If you want to go the library route, use an alternative like Luxon. It is significantly smaller (because of its clever use of the Intl object and support for tree-shaking) and just as versatile as moment.js.

To go back 5 days from today in Luxon, you would do:

import { DateTime } from 'luxon'

DateTime.now().minus({ days: 5 });
Assertive answered 6/4, 2021 at 15:45 Comment(0)
W
5

function addDays (date, daysToAdd) {
  var _24HoursInMilliseconds = 86400000;
  return new Date(date.getTime() + daysToAdd * _24HoursInMilliseconds);
};

var now = new Date();

var yesterday = addDays(now, - 1);

var tomorrow = addDays(now, 1);
Whispering answered 20/6, 2017 at 10:45 Comment(0)
S
4

See the following code, subtract the days from the current date. Also, set the month according to substracted date.

var today = new Date();
var substract_no_of_days = 25;

today.setTime(today.getTime() - substract_no_of_days* 24 * 60 * 60 * 1000);
var substracted_date = (today.getMonth()+1) + "/" +today.getDate() + "/" + today.getFullYear();

alert(substracted_date);
Sinuation answered 21/5, 2019 at 14:1 Comment(1)
This might not matter 99.9% of the time, but due to time adjustments not all days are 86.4m milliseconds. This could distort the result by +/- 1 dayProtect
D
2

I have created a function for date manipulation. you can add or subtract any number of days, hours, minutes.

function dateManipulation(date, days, hrs, mins, operator) {
   date = new Date(date);
   if (operator == "-") {
      var durationInMs = (((24 * days) * 60) + (hrs * 60) + mins) * 60000;
      var newDate = new Date(date.getTime() - durationInMs);
   } else {
      var durationInMs = (((24 * days) * 60) + (hrs * 60) + mins) * 60000;
      var newDate = new Date(date.getTime() + durationInMs);
   }
   return newDate;
 }

Now, call this function by passing parameters. For example, here is a function call for getting date before 3 days from today.

var today = new Date();
var newDate = dateManipulation(today, 3, 0, 0, "-");
Detailed answered 12/7, 2018 at 12:42 Comment(0)
N
2

Use MomentJS.

function getXDaysBeforeDate(referenceDate, x) {
  return moment(referenceDate).subtract(x , 'day').format('MMMM Do YYYY, h:mm:ss a');
}

var yourDate = new Date(); // let's say today
var valueOfX = 7; // let's say 7 days before

console.log(getXDaysBeforeDate(yourDate, valueOfX));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
Novelize answered 17/7, 2018 at 13:5 Comment(0)
D
1

The top answers led to a bug in my code where on the first of the month it would set a future date in the current month. Here is what I did,

curDate = new Date(); // Took current date as an example
prvDate = new Date(0); // Date set to epoch 0
prvDate.setUTCMilliseconds((curDate - (5 * 24 * 60 * 60 * 1000))); //Set epoch time
Deputize answered 10/6, 2016 at 13:59 Comment(0)
K
1

I like the following because it is one line. Not perfect with DST changes but usually good enough for my needs.

var fiveDaysAgo = new Date(new Date() - (1000*60*60*24*5));
Kaylor answered 31/3, 2017 at 0:21 Comment(0)
C
1

Using Modern JavaScript function syntax

const getDaysPastDate = (daysBefore, date = new Date) => new Date(date - (1000 * 60 * 60 * 24 * daysBefore));

console.log(getDaysPastDate(1)); // yesterday
Carbolize answered 30/11, 2018 at 8:25 Comment(0)
B
0

A easy way to manage dates is use Moment.js

You can use add. Example

var startdate = "20.03.2014";
var new_date = moment(startdate, "DD.MM.YYYY");
new_date.add(5, 'days'); //Add 5 days to start date
alert(new_date);

Docs http://momentjs.com/docs/#/manipulating/add/

Bula answered 13/6, 2016 at 14:18 Comment(0)
D
0

for me all the combinations worked fine with below code snipplet , the snippet is for Angular-2 implementation , if you need to add days , pass positive numberofDays , if you need to substract pass negative numberofDays

function addSubstractDays(date: Date, numberofDays: number): Date {
let d = new Date(date);
return new Date(
    d.getFullYear(),
    d.getMonth(),
    (d.getDate() + numberofDays)
);
}
Darrickdarrill answered 30/3, 2017 at 0:47 Comment(0)
S
0

I get good mileage out of date.js:

http://www.datejs.com/

d = new Date();
d.add(-10).days();  // subtract 10 days

Nice!

Website includes this beauty:

Datejs doesn’t just parse strings, it slices them cleanly in two

Satrap answered 8/3, 2018 at 15:6 Comment(0)
M
0

If you want to both subtract a number of days and format your date in a human readable format, you should consider creating a custom DateHelper object that looks something like this :

var DateHelper = {
    addDays : function(aDate, numberOfDays) {
        aDate.setDate(aDate.getDate() + numberOfDays); // Add numberOfDays
        return aDate;                                  // Return the date
    },
    format : function format(date) {
        return [
           ("0" + date.getDate()).slice(-2),           // Get day and pad it with zeroes
           ("0" + (date.getMonth()+1)).slice(-2),      // Get month and pad it with zeroes
           date.getFullYear()                          // Get full year
        ].join('/');                                   // Glue the pieces together
    }
}

// With this helper, you can now just use one line of readable code to :
// ---------------------------------------------------------------------
// 1. Get the current date
// 2. Subtract 5 days
// 3. Format it
// 4. Output it
// ---------------------------------------------------------------------
document.body.innerHTML = DateHelper.format(DateHelper.addDays(new Date(), -5));

(see also this Fiddle)

Malleus answered 15/4, 2018 at 23:20 Comment(0)
W
0

To calculate relative time stamps with a more precise difference than whole days, you can use Date.getTime() and Date.setTime() to work with integers representing the number of milliseconds since a certain epoch—namely, January 1, 1970. For example, if you want to know when it’s 17 hours after right now:

const msSinceEpoch = (new Date()).getTime();
const fortyEightHoursLater = new Date(msSinceEpoch + 48 * 60 * 60 * 1000).toLocaleString();
const fortyEightHoursEarlier = new Date(msSinceEpoch - 48 * 60 * 60 * 1000).toLocaleString();
const fiveDaysAgo = new Date(msSinceEpoch - 120 * 60 * 60 * 1000).toLocaleString();

console.log({msSinceEpoch, fortyEightHoursLater, fortyEightHoursEarlier, fiveDaysAgo})

reference

Wallaroo answered 19/1, 2021 at 18:3 Comment(0)
I
0
function daysSinceGivenDate (date) {
  const dateInSeconds = Math.floor((new Date().valueOf() - date.valueOf()) / 1000);
  const oneDayInSeconds = 86400;

  return Math.floor(dateInSeconds / oneDayInSeconds); // casted to int
};

console.log(daysSinceGivenDate(new Date())); // 0
console.log(daysSinceGivenDate(new Date("January 1, 2022 03:24:00"))); // relative...
Inapposite answered 10/1, 2022 at 15:41 Comment(0)
L
0

First arg is the date to start with and second is how mush day you want to increase or reduce to the date

example (1)- pass -1 to reduce date by one day

example (2)- pass 1 to increase date by one day

const EditDay = (date: Date, num: number): Date => {
 return new Date(date.getTime() + num * 24 * 60 * 60 * 1000)
}
Lambert answered 29/1, 2023 at 22:24 Comment(0)
A
0

Using jQuery:

minDate: new Date(), minDate:-NumberOfDays

Armadillo answered 7/6, 2023 at 6:15 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Kay
H
0

I made a simple function to quickly calculate the "time ago" logic.

/**
 * Helper function to calculate "xx times ago"
 * 
 * @param timestamp The Unix timestamp in milliseconds to compare against the current (now) timestamp
 * @returns The date formatted in "time ago". E.g. 7 days ago
 */
function calcTimeAgo (timestamp) {
  const now = new Date().getTime()
  const diff = now - timestamp

  let text

  if (diff < 60000) {
    text = 'moments'
  } else if (diff < 3.6e+6) {
    const min = Math.round((diff) / 60000)
    text = min === 1
      ? `${min} minute`
      : `${min} minute`
  } else if (diff < 8.64e+7) {
    const hours = Math.round((diff) / 3.6e+6)
    text = hours === 1
      ? `${hours} hour`
      : `${hours} hours`
  } else {
    const days = Math.round((diff) / 8.64e+7)
    text = days === 1
      ? `${days} day`
      : `${days} days`
  }

  return `${text} ago`
}

calcTimeAgo(new Date() - 2.592e+8) // returns "3 days ago"
Hypoxanthine answered 28/10, 2023 at 8:20 Comment(0)
H
-1

When setting the date, the date converts to milliseconds, so you need to convert it back to a date:

This method also take into consideration, new year change etc.

function addDays( date, days ) {
    var dateInMs = date.setDate(date.getDate() - days);
    return new Date(dateInMs);
}

var date_from = new Date();
var date_to = addDays( new Date(), parseInt(days) );
Henrique answered 13/1, 2016 at 8:49 Comment(1)
need: var dateInMs = date.setDate(date.getDate() + days);Irbm
P
-1

You can using Javascript.

var CurrDate = new Date(); // Current Date
var numberOfDays = 5;
var days = CurrDate.setDate(CurrDate.getDate() + numberOfDays);
alert(days); // It will print 5 days before today

For PHP,

$date =  date('Y-m-d', strtotime("-5 days")); // it shows 5 days before today.
echo $date;

Hope it will help you.

Planetoid answered 14/5, 2016 at 8:59 Comment(0)
S
-1

I converted into millisecond and deducted days else month and year won't change and logical

var numberOfDays = 10;//number of days need to deducted or added
var date = "01-01-2018"// date need to change
var dt = new Date(parseInt(date.substring(6), 10),        // Year
              parseInt(date.substring(3,5), 10) - 1, // Month (0-11)
              parseInt(date.substring(0,2), 10));
var new_dt = dt.setMilliseconds(dt.getMilliseconds() - numberOfDays*24*60*60*1000);
new_dt = new Date(new_dt);
var changed_date = new_dt.getDate()+"-"+(new_dt.getMonth()+1)+"-"+new_dt.getFullYear();

Hope helps

Shiekh answered 19/3, 2018 at 11:0 Comment(0)
L
-1

var date = new Date();
var day = date.getDate();
var mnth = date.getMonth() + 1;

var fDate = day + '/' + mnth + '/' + date.getFullYear();
document.write('Today is: ' + fDate);
var subDate = date.setDate(date.getDate() - 1);
var todate = new Date(subDate);
var today = todate.getDate();
var tomnth = todate.getMonth() + 1;
var endDate = today + '/' + tomnth + '/' + todate.getFullYear();
document.write('<br>1 days ago was: ' + endDate );
Lebar answered 17/7, 2018 at 12:49 Comment(1)
You should always comment your code/answer as some people might not get what you're doingEstivate
C
-1

Try something like this

dateLimit = (curDate, limit) => {
    offset  = curDate.getDate() + limit
    return new Date( curDate.setDate( offset) )
}

currDate could be any date

limit could be the difference in number of day (positive for future and negative for past)

Cupid answered 12/2, 2019 at 7:5 Comment(0)
M
-2

var d = new Date();

document.write('Today is: ' + d.toLocaleString());

d.setDate(d.getDate() - 31);

document.write('<br>5 days ago was: ' + d.toLocaleString());
Mercaptopurine answered 29/7, 2016 at 5:26 Comment(0)
C
-3
var daysToSubtract = 3;
$.datepicker.formatDate('yy/mm/dd', new Date() - daysToSubtract) ;
Collin answered 11/9, 2014 at 17:8 Comment(0)
S
-3

This will give you last 10 days result 110% working you will not get any type of issue

var date = new Date();
var day=date.getDate();
var month=date.getMonth() + 1;
var year=date.getFullYear();
var startDate=day+"/"+month+"/"+year;
var dayBeforeNineDays=moment().subtract(10, 'days').format('DD/MM/YYYY');
startDate=dayBeforeNineDays;
var endDate=day+"/"+month+"/"+year;

you can change the subtract days according to your requirements

Socialminded answered 15/3, 2019 at 4:6 Comment(0)
S
-5
var my date = new Date().toISOString().substring(0, 10);

it can give you only date like 2014-06-20. hope will help

Superego answered 20/6, 2014 at 16:11 Comment(1)
Where is the subtracting of X days as OP required?Barely

© 2022 - 2024 — McMap. All rights reserved.