2758. Next Day π
Description
Write code that enhances all date objects such that you can call the date.nextDay() method on any date object and it will return the next day in the format YYYY-MM-DD as a string.
Example 1:
Input: date = "2014-06-20"
Output: "2014-06-21"
Explanation:
const date = new Date("2014-06-20");
date.nextDay(); // "2014-06-21"
Example 2:
Input: date = "2017-10-31" Output: "2017-11-01" Explanation: The day after 2017-10-31 is 2017-11-01.
Constraints:
new Date(date)is a valid date object
Solutions
Solution 1
Thinking
Extend \(Date\) with a method that returns the next calendar day as \(YYYY\text{-}MM\text{-}DD\). Hand-rolled month lengths and leap years are easy to get wrong.
Copy the timestamp, \(setDate(getDate()+1)\), and slice the ISO date so the original instance is left unchanged.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |