Warning: session_start(): open(/tmp/sess_569528cc9de0fbded2264d9869eea8dc, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
Increment A Date By 7 Days In JavaScript - LearnShareIT

Increment A Date By 7 Days In JavaScript

Increment a Date by 7 Days in JavaScript

During JavaScript web application development, we may get a requirement to increment a date by a few days. This article will share how to increment a date by 7 days in JavaScript.

How to increment a date by 7 days in JavaScript?

We have the following two solutions:

  • Use the getDate() and setDate() method 
  • Use the moment.js library

Now, we will take a closer look at each solution with specific examples for each of them.

Use the getDate() and setDate() method

The getDate() method returns the day of the month (1 to 31) of a date.

Syntax:

Date.getDate()

Example:

const date = new Date();

// Returns the day of the month of the current date
const result = date.getDate();

console.log(result);

Output:

29

The setDate() method sets the day of the month (1 to 31) of the date and returns the number of milliseconds from 00:00:00 UTC of the set date to the current time.

Syntax:

Date.setDate(dateValue)

Parameter:

  • dateValue: day of the month (integer).

Example:

const d = new Date();
console.log(d.setDate(11)); // Return the number of milliseconds

Output:

1665452309126

Call the ‘new Date()’ method with the number of milliseconds received. We will get a complete date string.

Example:

const d = new Date();

// Sets the day of the month of d to 11
console.log(new Date(d.setDate(11)));

Output:

Tue Oct 11 2022 21:54:56 GMT-0400 (Eastern Daylight Time)

To increment a date by 7 days in JavaScript using the getDate() and setDate() methods. First, we use the getDate() method to get the day of the month specified, then add 7 to the received date. Next, we use the setDate() method to set the day of the month of the specified date.

Example:

// Increment current date by 7 days
const date1 = new Date();
const result1 = date1.setDate(date1.getDate() + 7); // Return the number of milliseconds

console.log(new Date(result1));

// Increment specified date by 7 days
const date2 = new Date('2022-11-01');
const result2 = date2.setDate(date2.getDate() + 7); // Return the number of milliseconds

console.log(new Date(result2));

Output:

Fri Nov 04 2022 22:07:30 GMT-0400 (Eastern Daylight Time)
Mon Nov 07 2022 20:00:00 GMT-0500 (Eastern Standard Time)

Use the moment.js library

Moment.js is a JavaScript date library for parsing, validating, manipulating, and formatting dates.

The moment.js library’s add() method adds the time to the current time.

Syntax:

moment().add(Number, String);

Parameter:

  • Number: Amount of time added.
  • String: Time unit (years, quarters, months, weeks, days, hours, minutes, seconds, and milliseconds).

The following example describes how to increment a date by 7 days using moment.js

Example:

const moment = require('moment');

// Increment current date by 7 days
console.log(moment().add(7, 'days'));

Output:

Moment<2022-11-05T02:11:02+00:00>

Summary

This article has shown how to increment a date by 7 days in JavaScript. I hope the information in this article will be helpful to you. If you have any problems, please comment below. I will answer. Thank you for reading!

Maybe you are interested:

Leave a Reply

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