When building tools that work with dates, you may want to provide a button that acts as a shortcut to increase a date by one week. Internally, your application still works with actual dates and you may want to calculate the date in one week.
Node.js Series Overview
- Callback and Promise Support in your Node.js Modules
- Increase the Memory Limit for Your Process
- Why You Should Add “node” in Your Travis Config
- Create a PDF from HTML with Puppeteer and Handlebars
- Create Your Own Custom Error
- Extend Multiple Classes (Multi Inheritance)
- Get a File’s Created Date
- Get a File’s Last Modified/Updated Date
- Human-Readable JSON.stringify() With Spaces and Line Breaks
- Write a JSON Object to a File
- How to Create an Empty File
- Run Async Functions/Promises in Sequence
- Run Async Functions/Promises in Parallel
- Run Async Functions in Batches
- How to Merge Objects
Date class provides dozens of methods to manipulate a date instance. Increasing a date instance by one week is basically a calculation of adding seven days to today:
const nextWeek = new Date() // add 7 days to the current date nextWeek.setDate(new Date().getDate() + 7) console.log(nextWeek) // 2020-01-03T11:08:14.918Z
Starting from today, you may retrieve the current days in the month using the
.getDate() method. You can then go ahead and add 7 more days to increase the days by one week.
That’s it! Enjoy dates and make it rock!