Practice Day of the Year
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
Given a string date representing a Gregorian calendar date formatted as YYYY-MM-DD, return the day number of the year.
Source: Day of the Year on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
date = "2019-01-09"
9
January is the first month, so no complete earlier months contribute days. Add its day-of-month value, 9, to get day number 9. The count is one-based: January 1 is day 1, so January 9 is not day 8. Parse the month and day as numbers; their leading zeros are formatting, not additional digits in the count.
# Example 2
date = "2019-02-10"
41
January contributes 31 completed days, and February contributes its first 10 days through the given date. The day number is therefore 31 + 10 = 41. Add only complete months before the target month, then add the given day; adding all of February would overshoot. Leap-day adjustments cannot affect a date this early in February because February 29, when present, has not yet occurred.
Constraints
Related LeetCode Problems
Practice more problems similar to Day of the Year.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.