#2758
Easy JavaScript Next day
85.8% acceptance
Mar 31, 2026
13
1
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.
Solution
TypeScript
Time O(1)
Space O(1)
interface Date {
nextDay(): string;
}
Date.prototype.nextDay = function(): string {
const next = new Date(this);
next.setDate(next.getDate() + 1);
const y = next.getFullYear();
const m = String(next.getMonth() + 1).padStart(2, '0');
const d = String(next.getDate()).padStart(2, '0');
return `${y}-${m}-${d}`;
}
/**
* const date = new Date("2014-06-20");
* date.nextDay(); // "2014-06-21"
*/