# 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, **1 ^{st}** January is the

**1**day of the year,

^{st}**2**January is the

^{nd}**2**day of the year,

^{nd}**1**February is the

^{st}**32**day of the year and so on.

^{nd}**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**i**month.^{th} - 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**29**.^{th}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++

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

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

9

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

## Recommended Posts:

- Date after adding given number of days to the given date
- Find day of the week for a given date
- Zeller's Congruence | Find the Day for a Date
- Count of elements on the left which are divisible by current element
- Find the largest number smaller than integer N with maximum number of set bits
- Find minimum number to be divided to make a number a perfect square
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Find smallest possible Number from a given large Number with same count of digits
- Find the minimum number to be added to N to make it a prime number
- Given number of matches played, find number of teams in tournament
- Find the largest good number in the divisors of given number N
- Find the smallest number whose digits multiply to a given number n
- Find count of digits in a number that divide the number
- Find the number of ways to divide number into four parts such that a = c and b = d
- Find the total number of composite factor for a given number

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.