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.
This approach involves creating a static list of the total days for each month. Parse the given date to extract the year, month, and day. Check if the year is a leap year to adjust February's days if necessary. Sum the days from previous months and add the current month's day to find the day of the year.
The function isLeapYear checks if a year is a leap year. The dayOfYear function parses the date string, then computes the day of the year by summing month days and adjusts if it's a leap year.
Time Complexity: O(1) because the number of months is constant.
Space Complexity: O(1) because we're using a fixed-size array and a few integers.
This approach utilizes built-in or third-party date libraries (where applicable) to parse the input string and directly acquire the day of the year. This reduces manual effort by leveraging efficient, existing functions.
The code snippet uses JavaScript Date to parse and compute the day of the year by calculating the difference in days from the start of the year.
JavaScript
Time Complexity: O(1).
Space Complexity: O(1).
According to the problem, the given date is in the Gregorian calendar, so we can directly calculate which day of the year it is.
First, calculate the year, month, and day from the given date, denoted as y, m, d.
Then, calculate the number of days in February of that year according to the leap year rules of the Gregorian calendar. There are 29 days in February of a leap year and 28 days in a non-leap year.
The leap year calculation rule is: the year can be divided by
400, or the year can be divided by4but not by100.
Finally, calculate which day of the year it is according to the given date, that is, add up the number of days in each previous month, and then add the number of days in the current month.
The time complexity is O(1), and the space complexity is O(1).
Python
Java
C++
Go
TypeScript
JavaScript
| Approach | Complexity |
|---|---|
| Using Predefined Days in Months | Time Complexity: O(1) because the number of months is constant. |
| Using Date Libraries for Direct Calculation | Time Complexity: O(1). |
| Direct Calculation | — |
| 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 |
LeetCode#1154 Day of the Year - Python • CodeJulian • 1,277 views views
Watch 9 more video solutions →Practice Day of the Year with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor