Most of the time, while working with different data type objects, there are not many issues but when it comes to Date and Time, there are always multiple test cases to pass.
In this blog, I would like to explain how to find a future or past date using JavaScript.
We can get the current date and time using new Date() constructor.
Many times, while programming, we get the requirement to send a reminder mail after 7, 14, or 30 days but most of the time, we do a manual calculation of adding +7 days or more. This will be a logical answer.
Here, consider the date is the 30th of any month and if 7 days are added, then the results will be illogical. Thus, the following code snippet will explain how to handle such situation.
Note
Months are zero indexed.
- var reminderDate = new Date()
- var currentDate = new Date(Date.now())
- currentDate
-
- reminderDate.setDate(currentDate.getDate()+30)
-
- reminderDate
-
In the above code snippet, when we added 30 days from the current date, the month part is automatically taken care of.