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.
Input: str = “2019-01-09”
Input: str = “2003-03-01”
- 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 + days + … + 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:
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
- 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 firstname.lastname@example.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.