JavaScript Basics

JavaScript Advanced

JavaScript Arrays

JavaScript Functions

JavaScript Objects

JavaScript DOM

JavaScript String

How to add 2 weeks or 14 days to date in JavaScript?

To add 14 days to date, follow the following steps:

  1. Get the day of the month of the given Date object by calling the getDate() method.
  2. Add 14 days to the day obtained in step 1 and pass the resulting value to the setDate() method.

In this way, you can increment a date by 14 days or 2 weeks.

let d = new Date('2021-08-11');
console.log(d); //Wed Aug 11 2021

d.setDate(d.getDate() + 14);
console.log(d); //Wed Aug 25 2021

Note: In JavaScript date and time are represented by the Date class.

The getDate() method is used to get the day of the month. It returns an integer value between 1 and 31.

The setDate() method is used to set the day of the month.

If adding 14 days to the Date result into the next month or year, then the JavaScript Date object will automatically increment the month or year accordingly.

let d1 = new Date('2022-01-27');

d1.setDate(d1.getDate() + 14);
console.log(d1.toString()); //Thu Feb 10 2022

let d2 = new Date('2021-12-25');

d2.setDate(d2.getDate() + 14);
console.log(d2.toString()); //Sat Jan 08 2022

Recommended Posts