Open In App

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

Last Updated : 02 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given 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 the number of the day in the current month and print the final count.

Below is the implementation of the above approach: 

C++




// 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;
}


Java




// 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


Python3




# 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


C#




// 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


Javascript




<script>
 
// Javascript implementation of the approach
 
var 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
function dayOfYear( date)
{
    // Extract the year, month and the
    // day from the date string
    var year = parseInt(date.substring(0, 4));
    var month = parseInt(date.substring(5, 6));
    var day = 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
var date = "2019-01-09";
document.write( dayOfYear(date));
 
// This code is contributed by rutvik_56.
</script>


Output

9

Time Complexity: O(1)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads