Next Day - Problem
Enhance JavaScript Date Objects with Next Day Functionality
Your task is to extend the JavaScript Date prototype by adding a custom
Goal: Write code that allows any Date object to call
Input: Any valid Date object
Output: String representing the next day in ISO format
Example:
This problem tests your understanding of JavaScript prototypes, date manipulation, and handling edge cases like month boundaries, leap years, and year transitions.
Your task is to extend the JavaScript Date prototype by adding a custom
nextDay() method that returns the next calendar day in ISO format.Goal: Write code that allows any Date object to call
date.nextDay() and receive the next day as a string in YYYY-MM-DD format.Input: Any valid Date object
Output: String representing the next day in ISO format
Example:
const today = new Date('2023-12-31');console.log(today.nextDay()); // "2024-01-01"This problem tests your understanding of JavaScript prototypes, date manipulation, and handling edge cases like month boundaries, leap years, and year transitions.
Input & Output
example_1.js โ Year Boundary
$
Input:
const date = new Date('2023-12-31');
date.nextDay();
โบ
Output:
"2024-01-01"
๐ก Note:
December 31st rolls over to January 1st of the next year, demonstrating year boundary handling.
example_2.js โ Month Boundary
$
Input:
const date = new Date('2023-04-30');
date.nextDay();
โบ
Output:
"2023-05-01"
๐ก Note:
April 30th (last day of April) advances to May 1st, showing month boundary transition.
example_3.js โ Leap Year
$
Input:
const date = new Date('2024-02-28');
date.nextDay();
โบ
Output:
"2024-02-29"
๐ก Note:
In leap year 2024, February 28th advances to February 29th instead of March 1st.
Constraints
-
The method should be added to
Date.prototype - Return format must be exactly YYYY-MM-DD
- Must handle all edge cases: leap years, month boundaries, year transitions
- Should not mutate the original Date object
- Input dates are valid JavaScript Date objects
Visualization
Tap to expand
Understanding the Visualization
1
Copy the Calendar
Make a copy so we don't mess up the original date
2
Tell it 'Add One Day'
JavaScript's Date.setDate() is like having a smart assistant
3
Magic Happens
December 32nd? No problem! โ January 1st automatically
4
Get Clean Format
toISOString() gives us perfect YYYY-MM-DD format
Key Takeaway
๐ฏ Key Insight: JavaScript's Date.setDate() automatically handles calendar arithmetic - when you set day 32 of December, it becomes day 1 of January next year!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code