Python programming implements the method of entering a date of a year to calculate the date of that year

  • 2020-05-30 20:29:14
  • OfStack

This example shows how the programming implementation of Python inputs a date of a year to calculate the date of that year. I will share it with you for your reference as follows:

# based on Python3

1 way:


def is_leap_year(year): #  A leap year is returned True Otherwise return False
  if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
    return True
  else:
    return False
def function1(year, month, day): #  Calculate the given date to be that 1 The day of the year 
  leap_year = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  no_leap_year = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  if is_leap_year(year):
    result = sum(leap_year[:month - 1]) + day
  else:
    result = sum(no_leap_year[:month - 1]) + day
  return result

But if you're the one with the need, it doesn't have to be that complicated. That's because Python has built-in built-in time and date handling functions.


import datetime
import time
def function2(year, month, day): #  Direct use of Python Built-in module datetime Format conversion function to get results 
  date = datetime.date(year, month, day)
  return date.strftime('%j')

It is important to note that in the above method, the parameters of the function are integers of year, month and day. If you want to pass in a string, such as "2016-10-1", you need to do something with the string first.

Likewise, you can do it yourself or with built-in functions.


#  Suppose the input format is a string (read a string from the command line, for example) 2016-10-1 ), the input needs to be processed first 
_input = '2016-10-1'
_year1 = int(_input.split('-')[0])
_month1 = int(_input.split('-')[1])
_day1 = int(_input.split('-')[2])
#  You can use it, too datetime Is a built-in method for formatting 
t = time.strptime(_input, '%Y-%m-%d')
_year2 = t.tm_year
_month2 = t.tm_mon
_day2 = t.tm_mday

Below is the complete code. The results of "2016-10-1" are 275.


import datetime
import time
def is_leap_year(year): #  A leap year is returned True Otherwise return False
  if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
    return True
  else:
    return False
def function1(year, month, day): #  Calculate the given date to be that 1 The day of the year 
  leap_year = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  no_leap_year = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  if is_leap_year(year):
    result = sum(leap_year[:month - 1]) + day
  else:
    result = sum(no_leap_year[:month - 1]) + day
  return result
def function2(year, month, day): #  Direct use of Python Built-in module datetime Format conversion function to get results 
  date = datetime.date(year, month, day)
  return date.strftime('%j')
print(function1(2016, 10, 1))
print(function2(2016, 10, 1))
#  Suppose the input format is a string (read a string from the command line, for example) 2016-10-1 ), the input needs to be processed first 
_input = '2016-10-1'
_split = _input.split('-')
_year1 = int(_split[0])
_month1 = int(_split[1])
_day1 = int(_split[2])
print(function1(_year1, _month1, _day1))
print(function2(_year1, _month1, _day1))
#  You can use it, too datetime Is a built-in method for formatting 
t = time.strptime(_input, '%Y-%m-%d')
_year2 = t.tm_year
_month2 = t.tm_mon
_day2 = t.tm_mday
print(function1(_year2, _month2, _day2))
print(function2(_year2, _month2, _day2))


#  It turns out that I'm getting a little bit more complicated for function writing, if the input is actually a string 1 A few words is good 
import time
_input = '2016-10-1'
#  As shown in the Python The date and string formats are converted to each other  https://www.ofstack.com/article/66019.htm
t = time.strptime(_input, '%Y-%m-%d')
print(time.strftime('%j',t))

PS: here are some other online tools for calculating dates and days:

Online date/days calculator:
http://tools.ofstack.com/jisuanqi/date_jisuanqi

Online calendar:
http://tools.ofstack.com/bianmin/wannianli

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

More about Python related content interested readers to view this site project: "skills summary Python date and time", "Python URL skills summary", "Python pictures skills summary", "Python data structure and algorithm tutorial", "Python Socket programming skills summary", "Python function using techniques", "Python string skills summary", "Python introduction and advanced tutorial" and "Python file and directory skills summary"

I hope this article is helpful to you Python programming.


Related articles: