- By using the setDate() and getDate() methods
- By using the Date constructor with the getTime() method
- By using a third-party library like Moment.js or date-fns
Let’s explore each…
#1 – By using the setDate() and getDate() methods
let someDate = new Date(); someDate.setDate(someDate.getDate() + 5);
How it works
The setDate() method sets the day of the Date object relative to the beginning of the currently set month, while the getDate() method returns the day of the month from the Date object. By adding a number to the result of getDate(), we essentially “move” the date forward by that many days.
- First, a Date object is created with the current date and time using the ‘new Date()’ command.
- The getDate() method is then called on the ‘someDate’ object, which returns the current day of the month (from 1-31).
- We add the number of days we want to this returned value. In our example, we add 5 days.
- Finally, the setDate() method is called on the ‘someDate’ object again, setting the day of the month to the newly calculated value. This effectively “moves” the date forward by the desired number of days.
#2 – By using the Date constructor with the getTime() method
let currentDate = new Date(); let tenDaysLater = new Date(currentDate.getTime() + 10 * 24 * 60 * 60 * 1000);
How it works
The above code creates a new Date instance representing the current date and time. It then manipulates this timestamp to compute a new date that is 10 days later. The new timestamp is used to create a second Date instance representing the future date.
Step 1: The Date constructor with no parameters,
new Date(), creates a new Date object with the current date and time.
Step 2: The
getTime()method is called on this Date object. This method returns the number of milliseconds since the Unix Epoch (1 January 1970 00:00:00 UTC), representing the current timestamp.
Step 3: To this timestamp, we add the number of milliseconds in 10 days. This is calculated as
10 * 24 * 60 * 60 * 1000, which represents 10 days’ worth of milliseconds.
Step 4: We use the updated timestamp to construct a new Date object,
new Date(currentDate.getTime() + 10 * 24 * 60 * 60 * 1000). This represents the date and time 10 days in the future.
#3 – By using a third-party library like Moment.js or date-fns
// Using Moment.js const moment = require('moment'); let date = moment().add(5, 'days'); // Using date-fns const dateFns = require('date-fns'); let date = dateFns.addDays(new Date(), 5);
How it works:
- The ‘moment()’ function in Moment.js and ‘new Date()’ in date-fns gets the current date and time.
- Then, we use the ‘add’ function in Moment.js and ‘addDays’ in date-fns to add a specific number of days to the current date.
- The ‘add’ function in Moment.js requires two parameters, the number of units to add and the type of unit (‘days’ in this case).
- The ‘addDays’ function in date-fns takes two parameters, the Date object to which days should be added, and the number of days to add.
- Finally, these functions return a new date object representing a future date.
Remember, practice is key to mastering this skill. So, keep experimenting with different dates and number of days to add. Happy coding!