Watch 10 video solutions for Day of the Year, a easy level problem involving Math, String. This walkthrough by CodeJulian has 1,277 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given a string date representing a Gregorian calendar date formatted as YYYY-MM-DD, return the day number of the year.
Example 1:
Input: date = "2019-01-09" Output: 9 Explanation: Given date is the 9th day of the year in 2019.
Example 2:
Input: date = "2019-02-10" Output: 41
Constraints:
date.length == 10date[4] == date[7] == '-', and all other date[i]'s are digitsdate represents a calendar date between Jan 1st, 1900 and Dec 31st, 2019.Problem Overview: You receive a date string in the format YYYY-MM-DD. The task is to return which day of the year that date represents. For example, 2019-02-10 corresponds to day 41. The main challenge is correctly handling month lengths and leap years.
Approach 1: Using Predefined Days in Months (O(1) time, O(1) space)
This approach manually calculates the day number by summing the number of days in all months before the given month, then adding the current day. Start by parsing the input string to extract year, month, and day. Maintain an array like [31,28,31,30,31,30,31,31,30,31,30,31] representing days in each month. If the year is a leap year (divisible by 400, or divisible by 4 but not 100), update February to 29 days. Iterate through months before the current month and accumulate their days, then add the given day. The algorithm performs a small fixed loop of at most 12 iterations, so the time complexity is effectively O(1) with O(1) extra space.
This method relies on basic math logic for leap year detection and simple parsing using string operations. Because it avoids heavy libraries and clearly demonstrates the leap year rule, it is the most common implementation used in interviews.
Approach 2: Using Date Libraries for Direct Calculation (O(1) time, O(1) space)
Many languages provide built-in date libraries that already handle calendar calculations. Instead of manually counting days, convert the input string into a date object and compute the difference between that date and the first day of the same year (YYYY-01-01). The difference in days plus one gives the day-of-year value. In JavaScript, for example, you can construct Date objects and subtract timestamps to obtain the day count.
This approach delegates leap year handling, month lengths, and calendar rules to the standard library. It keeps the implementation short and reduces the chance of logical errors. The complexity remains O(1) because all operations are constant-time calculations performed by the library.
Recommended for interviews: The predefined-days approach is typically expected. It shows that you understand leap year rules and can implement simple calendar math without relying on built-in utilities. The date-library solution is perfectly valid in production code, but interviewers often prefer seeing the manual calculation because it demonstrates reasoning with arrays, conditions, and basic math logic.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Using Predefined Days in Months | O(1) | O(1) | Best for interviews when implementing leap year logic manually |
| Using Date Libraries | O(1) | O(1) | Useful in production code where built-in date utilities are available |