Problem

Given a string date representing a Gregorian calendar date formatted as YYYY-MM-DD, return the day number of the year.

Examples

Example 1:

1
2
3
Input: date = "2019-01-09"
Output: 9
Explanation: Given date is the 9th day of the year in 2019.

Example 2:

1
2
Input: date = "2019-02-10"
Output: 41

Solution

Method 1 – Prefix Sum of Days in Months

Intuition

To find the day of the year for a given date, sum the days in all previous months and add the day of the current month. For leap years, February has 29 days.

Approach

  1. Parse the year, month, and day from the input string.
  2. Prepare an array with the number of days in each month.
  3. If the year is a leap year, set February to 29 days.
  4. Sum the days in all months before the given month and add the day.
  5. Return the result.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
public:
    int dayOfYear(string date) {
        int y = stoi(date.substr(0,4)), m = stoi(date.substr(5,2)), d = stoi(date.substr(8,2));
        vector<int> days = {31,28,31,30,31,30,31,31,30,31,30,31};
        if ((y%4==0 && y%100!=0) || (y%400==0)) days[1]=29;
        int ans = d;
        for (int i = 0; i < m-1; ++i) ans += days[i];
        return ans;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import "strconv"
func dayOfYear(date string) int {
    y, _ := strconv.Atoi(date[:4])
    m, _ := strconv.Atoi(date[5:7])
    d, _ := strconv.Atoi(date[8:])
    days := []int{31,28,31,30,31,30,31,31,30,31,30,31}
    if (y%4==0 && y%100!=0) || (y%400==0) { days[1]=29 }
    ans := d
    for i := 0; i < m-1; i++ { ans += days[i] }
    return ans
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
    public int dayOfYear(String date) {
        int y = Integer.parseInt(date.substring(0,4));
        int m = Integer.parseInt(date.substring(5,7));
        int d = Integer.parseInt(date.substring(8,10));
        int[] days = {31,28,31,30,31,30,31,31,30,31,30,31};
        if ((y%4==0 && y%100!=0) || (y%400==0)) days[1]=29;
        int ans = d;
        for (int i = 0; i < m-1; i++) ans += days[i];
        return ans;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
    fun dayOfYear(date: String): Int {
        val y = date.substring(0,4).toInt()
        val m = date.substring(5,7).toInt()
        val d = date.substring(8,10).toInt()
        val days = intArrayOf(31,28,31,30,31,30,31,31,30,31,30,31)
        if ((y%4==0 && y%100!=0) || (y%400==0)) days[1]=29
        var ans = d
        for (i in 0 until m-1) ans += days[i]
        return ans
    }
}
1
2
3
4
5
6
class Solution:
    def dayOfYear(self, date: str) -> int:
        y, m, d = int(date[:4]), int(date[5:7]), int(date[8:])
        days = [31,28,31,30,31,30,31,31,30,31,30,31]
        if (y%4==0 and y%100!=0) or (y%400==0): days[1]=29
        return d + sum(days[:m-1])
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
impl Solution {
    pub fn day_of_year(date: String) -> i32 {
        let y: i32 = date[0..4].parse().unwrap();
        let m: usize = date[5..7].parse().unwrap();
        let d: i32 = date[8..].parse().unwrap();
        let mut days = [31,28,31,30,31,30,31,31,30,31,30,31];
        if (y%4==0 && y%100!=0) || (y%400==0) { days[1]=29; }
        d + days[..m-1].iter().sum::<i32>()
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
    dayOfYear(date: string): number {
        const y = parseInt(date.slice(0,4)), m = parseInt(date.slice(5,7)), d = parseInt(date.slice(8));
        const days = [31,28,31,30,31,30,31,31,30,31,30,31];
        if ((y%4===0 && y%100!==0) || (y%400===0)) days[1]=29;
        let ans = d;
        for (let i = 0; i < m-1; i++) ans += days[i];
        return ans;
    }
}

Complexity

  • ⏰ Time complexity: O(1), only a constant number of operations.
  • 🧺 Space complexity: O(1), only a constant amount of extra space is used.