SoFunction
Updated on 2024-11-07

Example of how a Python custom function calculates what day of the year a given date is.

This article example describes the method of Python custom function to calculate the given date is the first day of the year. Shared for your reference, as follows:

Write a function that calculates what day of the year a given date is.

Satisfy the conditions for a leap year:

  • A leap year is a term used in the Gregorian calendar for a year that is divisible by 4 but not by 100, or that is divisible by 400.
  • A leap year is a year to make up for the time difference between the number of days in the year and the actual rotation cycle of the Earth caused by man-made calendar rules. The year in which the time difference is made up is called a leap year.
  • Any year in which there is a leap day in the solar calendar (February is the twenty-ninth day of the month); a leap year (the year in which the year is intercalated. Lunar Calendar each year compared with the return year of the difference in time and day); note that the leap year (a term in the Gregorian calendar) and the leap month (a term in the lunar calendar) are not directly related, the Gregorian calendar is only divided into a leap year and a flat year, a flat year has 365 days, while a leap year has 366 days (one more day in February); there may also be a leap month in a flat year (e.g., 2017 is a flat year, and the Lunar Calendar has a leap month, intercalary month of June).

The code is as follows:

#coding=utf-8
# Write a function that calculates what day of the year the given date is.
def count(year,month,day):
 count = 0
 # Determine if the year is a flat year or a leap year
 if year%400==0 or (year%4==0 and year%100!=0):
  print('%d year is a leap year, there are 29 days in February!'%year)
  li1 = [31,29,31,30,31,30,31,31,30,31,30,31]
  for i in range(month-1):
   count += li1[i]
  return count+day
 else:
  print('%d year is a flat year and there are 29 days in February!' % year)
  li2 = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  for i in range(month-1):
   count +=li2[i]
  return count+day
if __name__ == "__main__":
 year = int(input('Please enter the year:'))
 month = int(input('Please enter the month:'))
 day = int(input('Please enter the date:'))
 count = count(year,month,day)
 print('%d year %d month %d day is the %d day of the year!'%(year,month,day,count))

Run results:

Please enter the year: 2017
Please enter the month: 8
Please enter date: 17
August 17, 2017 was the 229th day of the year!

PS: Here are a few more online tools on date and day calculation for you to use:

Online date/day calculator:
http://tools./jisuanqi/date_jisuanqi

Online perpetual calendar calendars:
http://tools./bianmin/wannianli

Online lunar/solar calendar conversion tool:
http://tools./bianmin/yinli2yangli

Unix timestamp conversion tool:
http://tools./code/unixtime

Readers interested in more Python related content can check out this site's topic: theSummary of Python date and time manipulation techniques》、《Summary of Python mathematical operations techniques》、《Python Data Structures and Algorithms Tutorial》、《Python Socket Programming Tips Summary》、《Summary of Python function usage tips》、《Summary of Python string manipulation techniquesand thePython introductory and advanced classic tutorials

I hope that what I have said in this article will help you in Python programming.