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

08 Jun, 2021
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: 9Input: 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++
Java
Python3
C#
Javascript
// 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 dateint 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 codeint main(){    string date = "2019-01-09";    cout << dayOfYear(date);     return 0;}
// Java implementation of the approachclass 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 implementation of the approachdays = [31, 28, 31, 30, 31, 30,        31, 31, 30, 31, 30, 31]; # Function to return the day number# of the year for the given datedef 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 codeif __name__ == '__main__':    date = "2019-01-09";    print(dayOfYear(date)); # This code is contributed by Rajput-Ji
// C# implementation of the approachusing 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
<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 datefunction 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 codevar date = "2019-01-09";document.write( dayOfYear(date)); // This code is contributed by rutvik_56.</script>
9
 
ihritik
Rajput-Ji
rutvik_56
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Algorithm to solve Rubik's Cube
Program to convert a given number to words
Program to multiply two matrices
Modular multiplicative inverse
Program to print prime numbers from 1 to N.
Count ways to reach the n'th stair
Check if a number is Palindrome
Singular Value Decomposition (SVD)
Fizz Buzz Implementation
Find first and last digits of a number