Show problems as
Classic problem #1154EasyMathString

Day of the Year

Real-world version: Day Number Within the Year
Tracked in this browser only
Read Day of the Year on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A log stamp is given as date, a string in the form "YYYY-MM-DD".

Return which day of its own year that date is, counting the first of January as day 1.

A year has 366 days when it is a leap year and 365 otherwise. A year is a leap year when it divides by 4, except that a year dividing by 100 is not a leap year unless it also divides by 400.

Examples

Example 1

Input
date = "2020-12-31"
Output
366

The year 2020 was a leap year, so it held 366 days and its final day is day 366.

Example 2

Input
date = "2020-03-01"
Output
61

The year 2020 divides by 4 and not by 100, so February held 29 days, putting the first of March on day 61.

Example 3

Input
date = "1900-03-01"
Output
60

The year 1900 divides by 100 but not by 400, so it was not a leap year and February held 28 days.

Constraints

  • date.length == 10
  • date[4] and date[7] are both '-', and every other character is a digit
  • date names a real calendar date between the first of January 1900 and the last of December 2100

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def day_of_the_year(date: str) -> int:
Java
public int dayOfTheYear(String date)