[LeetCode]#1154. Day of the Year

Input: date = "2019-01-09"
Output: 9
Explanation: Given date is the 9th day of the year in 2019.
Input: date = "2019-02-10"
Output: 41
Input: date = "2003-03-01"
Output: 60
Input: date = "2004-03-01"
Output: 61
  • date.length == 10
  • date[4] == date[7] == '-', and all other date[i]'s are digits
  • date represents a calendar date between Jan 1st, 1900 and Dec 31, 2019.
import datetime
class Solution:
def dayOfYear(self, date):
year_check=int(date[0:4])
leap=0
if year_check % 400 ==0:
leap=1
if year_check % 4 ==0 and year_check % 100 !=0:
leap=1
day1 = datetime.datetime(int(date[0:4]), int(date[5:7]) , int(date[8:10]))
day2 = datetime.datetime(int(date[0:4]), int(12) , int(31))
ans=365-(day2-day1).days+leap
return ans

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store