Day of the Year

Easy
1287.

Day of the Year

Day of the Year is an easy coding problem on strings and math, asked in Amazon and TCS interviews.

date is a valid Gregorian calendar date written as YYYY-MM-DD. Return its day number within its year: January 1st is day 1, January 2nd is day 2, and so on.

A year is a leap year (with a 29-day February) when it is divisible by 4 but not by 100, or when it is divisible by 400.

Example 1

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

Example 2

Input
date = "2016-12-31"
Output
366
Explanation
2016 is a leap year.

Example 3

Input
date = "1900-03-01"
Output
60
Explanation
1900 is divisible by 100 but not by 400, so its February has 28 days.

Constraints

  • date.length == 10
  • date[4] == date[7] == '-', and all other characters are digits
  • date is a valid date between January 1st, 1900 and December 31st, 2019
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {string} date
 * @return {number}
 */
var dayOfYear = function(date) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Day of the Year — Solution in Python, Java & C++

Day of the Year solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem, asked at Amazon and TCS.

Day of the Year is an easy coding problem on strings and math, asked in Amazon and TCS interviews.

  • Difficulty: Easy
  • Topics: Strings, Math
  • Asked at: Amazon, TCS
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

date is a valid Gregorian calendar date written as YYYY-MM-DD. Return its day number within its year: January 1st is day 1, January 2nd is day 2, and so on.

A year is a leap year (with a 29-day February) when it is divisible by 4 but not by 100, or when it is divisible by 400.

Example 1

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

Example 2

Input: date = "2016-12-31"
Output: 366
Explanation: 2016 is a leap year.

Example 3

Input: date = "1900-03-01"
Output: 60
Explanation: 1900 is divisible by 100 but not by 400, so its February has 28 days.

Constraints

  • date.length == 10
  • date[4] == date[7] == '-', and all other characters are digits
  • date is a valid date between January 1st, 1900 and December 31st, 2019

Hints for Day of the Year

  1. Hint 1

    Split the string into year, month and day.

  2. Hint 2

    Add up the lengths of the months before the given month, then add the day.

  3. Hint 3

    February has 29 days only in leap years: divisible by 4 and not by 100, or divisible by 400.

How to solve Day of the Year

The day number is the total length of the earlier months plus the day of the month, with February's length decided by the leap-year rule.

Approach

  1. Parse year = date[0..4], month = date[5..7], day = date[8..10].
  2. Use month lengths [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], setting February to 29 in a leap year.
  3. Return day plus the sum of the first month - 1 lengths.

Why it works

Days are numbered consecutively through the year, so the date falls after every day of the earlier months. The Gregorian leap rule (every 4 years, except centuries not divisible by 400) is exactly what decides February's length.

Complexity

  • Time — O(1)
  • Space — O(1)

Pitfalls

  • 1900 is not a leap year, 2000 is — the century rule matters inside this range.
  • Parsing "09" with a base-guessing parser can read it as octal; always parse in base 10.
  • The leap day only shifts dates from March onwards.

Reference solution

Python

def dayOfYear(date: str) -> int:
    year, month, day = int(date[:4]), int(date[5:7]), int(date[8:])
    lengths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
        lengths[1] = 29
    return sum(lengths[:month - 1]) + day

Java

public static int dayOfYear(String date) {
    int year = Integer.parseInt(date.substring(0, 4));
    int month = Integer.parseInt(date.substring(5, 7));
    int day = Integer.parseInt(date.substring(8, 10));
    int[] lengths = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) lengths[1] = 29;
    for (int i = 0; i < month - 1; i++) day += lengths[i];
    return day;
}

C++

int dayOfYear(string date) {
    int year = stoi(date.substr(0, 4));
    int month = stoi(date.substr(5, 2));
    int day = stoi(date.substr(8, 2));
    int lengths[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) lengths[1] = 29;
    for (int i = 0; i < month - 1; i++) day += lengths[i];
    return day;
}

JavaScript

var dayOfYear = function(date) {
    var year = parseInt(date.substring(0, 4), 10);
    var month = parseInt(date.substring(5, 7), 10);
    var day = parseInt(date.substring(8, 10), 10);
    var lengths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    if ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0) lengths[1] = 29;
    for (var i = 0; i < month - 1; i++) day += lengths[i];
    return day;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 515 strings problems · the whole catalogue

Learn the technique: Strings