Skip to content

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.

Easy#1154
LeetCode Problem

Day of the Year

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

Input
date = "2019-01-09"
Output
9
Explanation

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

Input
date = "2019-02-10"
Output
41
Explanation

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

date.length == 10
date[4] == date[7] == '-', and all other date[i]'s are digits
date represents a calendar date between Jan 1st, 1900 and Dec 31st, 2019.

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.