
Problem Statement
In the given task, the objective is to extend the functionality of JavaScript date objects so that you can utilize a method named nextDay()
on any date object. The method should return the day following the date of the given object in a string format adhering to the standard "YYYY-MM-DD"
. This enhancement allows programmers to directly get the next day without performing manual calculations or adjustments based on month lengths, leap years, etc., making it a simpler and more efficient process.
Examples
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.
Approach and Intuition
To implement nextDay()
for all JavaScript Date
objects, we extend the prototype of the Date
class. JavaScript's Date
object internally handles leap years, month rollovers, and year transitions when the day of the month is increased beyond the valid limit for a given month.
Here’s the step-by-step breakdown:
Access the Date Value Use
this.getDate()
to retrieve the day of the month.Increment by One Day Call
this.setDate(this.getDate() + 1)
which updates the date to the next day. If the update exceeds the month’s last day, JavaScript’s engine automatically rolls the date to the next month or year.Format the New Date Use:
this.getFullYear()
for the year.this.getMonth() + 1
(since months are 0-indexed).this.getDate()
for the day of the month. Then format it as"YYYY-MM-DD"
usingpadStart(2, '0')
for month and day to ensure two-digit output.
Return the Result Return the formatted date string as the result of
nextDay()
.
Solutions
- JavaScript
Date.prototype.incrementDay = function() {
const monthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
const currentYear = this.getFullYear();
const currentMonth = this.getMonth();
const currentDay = this.getDate();
const leapYearCheck = (currentYear % 4 === 0 && currentYear % 100 !== 0) || (currentYear % 400 === 0);
if (leapYearCheck) {
monthDays[1] = 29; // Update February days for leap year
}
const daysInCurrentMonth = monthDays[currentMonth];
let futureYear = currentYear;
let futureMonth = currentMonth;
let upcomingDay = currentDay + 1;
// Adjust date when overflowing the current month
if (upcomingDay > daysInCurrentMonth) {
upcomingDay = 1;
futureMonth += 1;
if (futureMonth > 11) {
futureMonth = 0;
futureYear += 1;
}
}
// Construct and return the next day's date in YYYY-MM-DD format
const nextDate = ``{futureYear}-`{String(futureMonth + 1).padStart(2, '0')}-${String(upcomingDay).padStart(2, '0')}`;
return nextDate;
};
Expand the capabilities of JavaScript's Date prototype by adding the incrementDay
method, which calculates the date of the following day. The method considers different month lengths and leap years, ensuring accurate date computation. Follow these steps to implement and understand the functionality:
- Define an array of the days in each month.
- Retrieve the current year, month, and day from the
Date
object. - Adjust February's days based on leap year conditions. A leap year occurs if it's divisible by 4, not by 100, unless also by 400.
- Check if the incremented day exceeds the month's days. If so, reset to the first day of the next month, adjusting the month and year as needed.
- Construct and return the next day's date in a
YYYY-MM-DD
format using template literals and zero-padding for single-digit months and days.
This enhancement allows you to easily obtain the next day's date by simply calling incrementDay()
on any JavaScript Date object.
No comments yet.