Find the day number in the current year for the given date

Given a string str which represents a date formatted as YYYY-MM-DD, the task is to find the day number for the current year. For example, 1st January is the 1st day of the year, 2nd January is the 2nd day of the year, 1st February is the 32nd day of the year and so on.

Examples:

Input: str = “2019-01-09”
Output: 9



Input: str = “2003-03-01”
Output: 60

Approach:

  • Extract the year, month and the day from the given date and store them in variables year, month and day.
  • Create an array days[] where days[i] will store the number of days in the ith month.
  • Update count = days[0] + days[1] + … + days[month – 1] to get the count of all the past days of previous months.
  • If the given year is a leap year then increment this count by 1 in order to count 29th February.
  • Finally, add day to the count which is number of the day in the current month and print the final count.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
int days[] = { 31, 28, 31, 30, 31, 30,
               31, 31, 30, 31, 30, 31 };
  
// Function to return the day number
// of the year for the given date
int dayOfYear(string date)
{
    // Extract the year, month and the
    // day from the date string
    int year = stoi(date.substr(0, 4));
    int month = stoi(date.substr(5, 2));
    int day = stoi(date.substr(8));
  
    // If current year is a leap year and the date
    // given is after the 28th of February then
    // it must include the 29th February
    if (month > 2 && year % 4 == 0
        && (year % 100 != 0 || year % 400 == 0)) {
        ++day;
    }
  
    // Add the days in the previous months
    while (month-- > 0) {
        day = day + days[month - 1];
    }
    return day;
}
  
// Driver code
int main()
{
    string date = "2019-01-09";
    cout << dayOfYear(date);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG
{
    static int days [] = { 31, 28, 31, 30, 31, 30,
                           31, 31, 30, 31, 30, 31 };
      
    // Function to return the day number
    // of the year for the given date
    static int dayOfYear(String date)
    {
        // Extract the year, month and the
        // day from the date string
        int year = Integer.parseInt(date.substring(0, 4));
          
        int month = Integer.parseInt(date.substring(5, 7));
          
        int day = Integer.parseInt(date.substring(8));
          
        // If current year is a leap year and the date
        // given is after the 28th of February then
        // it must include the 29th February
        if (month > 2 && year % 4 == 0 && 
           (year % 100 != 0 || year % 400 == 0))
        {
            ++day;
        }
      
        // Add the days in the previous months
        while (--month > 0)
        {
            day = day + days[month - 1];
        }
        return day;
    }
      
    // Driver code
    public static void main (String[] args)
    {
        String date = "2019-01-09";
        System.out.println(dayOfYear(date));
    }
}
  
// This code is contributed by ihritik

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
days = [31, 28, 31, 30, 31, 30,
        31, 31, 30, 31, 30, 31];
  
# Function to return the day number
# of the year for the given date
def dayOfYear(date):
      
    # Extract the year, month and the
    # day from the date string
    year = (int)(date[0:4]);
    month = (int)(date[5:7]);
    day = (int)(date[8:]);
  
    # If current year is a leap year and the date
    # given is after the 28th of February then
    # it must include the 29th February
    if (month > 2 and year % 4 == 0 and 
       (year % 100 != 0 or year % 400 == 0)):
        day += 1;
  
    # Add the days in the previous months
    month -= 1;
    while (month > 0):
        day = day + days[month - 1];
        month -= 1;
    return day;
  
# Driver code
if __name__ == '__main__':
    date = "2019-01-09";
    print(dayOfYear(date));
  
# This code is contributed by Rajput-Ji

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG
{
    static int [] days = { 31, 28, 31, 30, 31, 30,
                           31, 31, 30, 31, 30, 31 };
      
    // Function to return the day number
    // of the year for the given date
    static int dayOfYear(string date)
    {
        // Extract the year, month and the
        // day from the date string
        int year = Int32.Parse(date.Substring(0, 4));
          
        int month = Int32.Parse(date.Substring(5, 2));
          
        int day = Int32.Parse(date.Substring(8));
          
        // If current year is a leap year and the date
        // given is after the 28th of February then
        // it must include the 29th February
        if (month > 2 && year % 4 == 0 && 
           (year % 100 != 0 || year % 400 == 0)) 
        {
            ++day;
        }
      
        // Add the days in the previous months
        while (--month > 0)
        {
            day = day + days[month - 1];
              
        }
        return day;
    }
      
    // Driver code
    public static void Main ()
    {
        String date = "2019-01-09";
        Console.WriteLine(dayOfYear(date));
    }
}
  
// This code is contributed by ihritik

chevron_right


Output:

9


My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : ihritik, Rajput-Ji

Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.