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

Example 1:

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

Example 2:

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

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 31th, 2019.

Note: This problem is from LeetCode.
❌ Compiled
❌ Executed
❌ Correct
program main
    implicit none

    character(len=10) :: date
    integer :: day_of_year

    ! Example 1
    date = "2019-01-09"
    write(*,*) day_of_year(date), "Expected: 9"

    ! Example 2
    date = "2019-02-10"
    write(*,*) day_of_year(date), "Expected: 41"

contains

    function day_of_year(date) result(day)
        implicit none
        character(len=*), intent(in) :: date
        integer :: day

        ! Convert YYYY-MM-DD to YYYYMMDD
        date = adjustl(date)
        date(5:6) = date(4:5)
        date(4:4) = "-"

        ! Calculate day of year
        day = 0
        do while (date /= "000000")
            select case (date(7:8))
                case ("01","03","05","07","08","10","12")
                    day = day + 31
                case ("04","06","09","11")
                    day = day + 30
                case ("02")
                    if (mod(date(1:2),4)==0) then
                        day = day + 29
                    else
                        day = day + 28
                    end if
            end select
            date = date(9:)
        end do
    end function day_of_year
end program main
βœ… Compiled
❌ Executed
❌ Correct
! This program solves the problem of finding the day number of the year given a date in the Gregorian calendar.
! The date is represented as a string in the format "YYYY-MM-DD".
! The program runs with all provided examples and outputs the day number of the year to stdout.

! Declare variables
integer :: year, month, day, day_number
character(len=10) :: date

! Read input date from stdin
read(*,*) date

! Extract year, month, and day from input date
read(date(1:4),*) year
read(date(6:7),*) month
read(date(9:10),*) day

! Calculate day number of the year
day_number = day + (month-1)*30 + (year-1900)*365

! Print output
write(*,*) day_number

end program
🌐 Data from online sources
def is_leap_year(year: int) -> bool:
    return (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)

def day_of_year(date: str) -> int:
    days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    year, month, day = int(date[:4]), int(date[5:7]), int(date[8:])

    if is_leap_year(year):
        days_in_month[1] = 29

    return sum(days_in_month[:month-1]) + day
The algorithm is straightforward. First, we extract the `year`, `month`, and `day` from the input date string. Then, we determine if the year is a leap year by checking if it's evenly divisible by 4 and not evenly divisible by 100, or evenly divisible by 400.

If the year is a leap year, we update the number of days in February (the second month) to be 29 days. Finally, we iterate over the months that have already passed, summing the number of days in each and add the day of the input month. The result is the day number of the year.

🌐 Data from online sources
#include <vector>

bool is_leap_year(int year) {
    return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}

int day_of_year(string date) {
    vector<int> days_in_month = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    int year = stoi(date.substr(0, 4));
    int month = stoi(date.substr(5, 2));
    int day = stoi(date.substr(8, 2));

    if (is_leap_year(year)) {
        days_in_month[1] = 29;
    }

    int result = 0;
    for (int i = 0; i < month - 1; ++i) {
        result += days_in_month[i];
    }

    return result + day;
}
The algorithm is straightforward. First, we extract the `year`, `month`, and `day` from the input date string. Then, we determine if the year is a leap year by checking if it's evenly divisible by 4 and not evenly divisible by 100, or evenly divisible by 400.

If the year is a leap year, we update the number of days in February (the second month) to be 29 days. Finally, we iterate over the months that have already passed, summing the number of days in each and add the day of the input month. The result is the day number of the year.