Add & Subtract Days Months Years To A Date In Javascript

Welcome to a tutorial on how to add and subtract days, months, years to a date in Javascript. New to Javascript and need to do some “date yoga”?

There are 2 common ways to add and subtract from a date in Javascript:

  1. Parse the date into an object and adjust accordingly.
    • var oDate = new Date("2008-09-10");
    • oDate.setDate(oDate.getDate() + 5);
    • oDate.setMonth(oDate.getMonth() - 3);
    • oDate.setYear(oDate.getFullYear() + 1);
  2. Parse the date into a Unix timestamp and calculate manually.
    • var unix = Math.floor(new Date("2007-08-09").getTime() / 1000);
    • var fiveDays = unix + (5 * 86400);
    • fiveDays  = new Date(fiveDays * 1000);
    • var oneYear = unix - (365 * 86400);
    • oneYear = new Date(oneYear * 1000);

That should cover the basics, but read on for more examples!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

REAL QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes Add Minus Date Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

QUICK NOTES

If you spot a bug, please feel free to comment below. I try to answer questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

 

ADD & MINUS TO DATE

All right, let us now get into the quick examples of how to add and subtract days, months, years from a given date in Javascript.

 

1) USING DATE OBJECT

1-date.html
 // (A) PARSE STRING INTO DATE OBJECT
var sDate = "2007-08-09";
var oDate = new Date(sDate);

// (B) ADD DAY, MONTHS, YEARS
// (B1) ADD 20 DAYS
oDate.setDate(oDate.getDate() + 20);
console.log(oDate);
// (B2) ADD 2 MONTHS
oDate.setMonth(oDate.getMonth() + 2);
console.log(oDate);
// (B3) ADD 1 YEARS
oDate.setYear(oDate.getFullYear() + 1);
console.log(oDate);

// (C) MINUS DAY, MONTHS, YEARS
// (C1) MINUS 7 DAYS
oDate.setDate(oDate.getDate() - 7);
console.log(oDate);
// (C2) MINUS 3 MONTHS
oDate.setMonth(oDate.getMonth() - 3);
console.log(oDate);
// (C3) MINUS 2 YEARS
oDate.setYear(oDate.getFullYear() - 2);
console.log(oDate);

This should be pretty self-explanatory.

  • We use the very convenient new Date("DATE STRING") to parse a given date string into a Date object.
  • Take note, if we just do a new Date(), it will default to the user’s current time.
  • Next, we combine the use of get and set functions to manipulate the date.
    • To add days – DATE.setDate(DATE.getDate() + DAYS);
    • To add months – DATE.setMonth(DATE.getMonth() + MONTHS);
    • Adding years – DATE.setYear(DATE.getFullYear() + YEARS);

Yep, simple as that.

 

 

2) USING UNIX TIMESTAMP

2-unix.html
// (A) PARSE STRING INTO UNIX TIMESTAMP
var sDate = "2007-08-09";
var unix = Math.floor(new Date(sDate).getTime() / 1000);

// (B) ADD DAY, MONTHS, YEARS
// (B1) ADD 5 DAYS
// 1 DAY = 86400 SECONDS
var days = unix + (5 * 86400);
console.log(new Date(days * 1000));

// (B2) ADD 2 MONTHS
// 1 MONTH MAY NOT BE 30 DAYS EXACTLY, USE DATE OBJECT FOR ACCURACY
var months = unix + (60 * 86400);
console.log(new Date(months * 1000));

// (B3) ADD 1 YEAR
var years = unix + (365 * 86400);
console.log(new Date(years * 1000));

// (C) ADD DAY, MONTHS, YEARS
// (C1) MINUS 10 DAYS
days = unix - (10 * 86400);
console.log(new Date(days * 1000));

// (C2) MINUS 1 MONTH
months = unix - (30 * 86400);
console.log(new Date(months * 1000));

// (C3) MINUS 1 YEAR
years = unix - (365 * 86400);
console.log(new Date(years * 1000));

For you guys who have not heard of it, the Unix Timestamp is simply the number of seconds that have elapsed since 1 Jan 1970.

  • To parse a given date into Unix Timestamp, we use var unix = new Date("DATE STRING").getTime();
  • This will return the Unix Timestamp in microseconds, which is why we further divide it by 1000 to get the number of seconds – unix = Math.floor(unix / 1000);
  • The rest of the calculations is pretty straightforward – One day has 60 seconds X 60 minutes X 24 hours = 86400 seconds.
  • So to add or subtract from the given date, we only have to add or minus the respective number of seconds. For example, 1 week before a given date is unix - (7 * 86400).

 

 

USEFUL BITS & LINKS

That’s all for the main tutorial, and here is a small section on some extras and links that may be useful to you.

 

INFOGRAPHIC CHEAT SHEET

Add Subtract From Date In Javascript (click to enlarge)

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *