Python programming implementation input a certain day of a year to calculate the day of the year is the day of the method, python a day

Source: Internet
Author: User

Python programming implementation input a certain day of a year to calculate the day of the year is the day of the method, python a day

The example in this article describes how to calculate the day of the year by entering the Python programming language. We will share this with you for your reference. The details are as follows:

# Based on Python3

One approach:

Def is_leap_year (year): # if a leap year is judged, True is returned. Otherwise, False if (year % 4 = 0 and year % 100 is returned! = 0) or year % 400 = 0: return True else: return Falsedef function1 (year, month, day): # Calculate the day of the year for a given date. 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, 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

However, if you have such a requirement, you do not need to be so complicated. Because Python has built-in complete time and date processing functions.

Import datetimeimport timedef function2 (year, month, day): # Use the datetime Format Conversion Function of the Python built-in module to obtain date = datetime. date (year, month, day) return date. strftime ('% J ')

Note that the function parameters in the preceding method are integers of the year, month, and day. If you want to input a string, such as "2016-10-1", you need to process the string first.

Similarly, you can also do it yourself or use built-in functions.

# If the input format is a string (for example, reading the string 2016-10-1 from the command line), you must first process the input content _ input = '2017-10-1 '_ year1 = int (_ input. split ('-') [0]) _ month1 = int (_ input. split ('-') [1]) _ day1 = int (_ input. split ('-') [2]) # Of course, you can also use the built-in datetime Method for format processing t = time. strptime (_ input, '% Y-% m-% D') _ year2 = t. tm_year_month1 = t. tm_mon_day2 = t. tm_mday

The following is the complete code. The test results for "2016-10-1" are 275.

Import datetimeimport timedef is_leap_year (year): # when determining a leap year, True is returned; otherwise, False is returned if (year % 4 = 0 and year % 100! = 0) or year % 400 = 0: return True else: return Falsedef function1 (year, month, day): # Calculate the day of the year for a given date. 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, 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 resultdef function2 (year, month, day): # Use the datetime Format Conversion Function of the Python built-in module to obtain the result date = datetime. date (year, month, day) return date. strftime ('% J') print (function1 (2016, 10, 1) print (function2 (2016, 10, 1 )) # If the input format is a string (for example, reading the string 2016-10-1 from the command line), you must first process the input content _ input = '2017-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 )) # Of course, you can also use the built-in datetime Method for format processing t = time. strptime (_ input, '% Y-% m-% D') _ year2 = t. tm_year_month1 = t. tm_mon_day2 = t. tm_mdayprint (function1 (_ year2, _ mon2, _ day2) print (function2 (_ year2, _ mon2, _ day2 ))
# Later I found that writing complex for programming functions, if the input is a string in fact a sentence is good import time_input = '2017-10-1 '# See Python date and string format conversion http://www.bkjia.com/article/66019.htmt = time. strptime (_ input, '% Y-% m-% D') print (time. strftime ('% J', t ))

PS: here we recommend several online tools for date and number of days computing:

Online date/day calculator:
Http://tools.jb51.net/jisuanqi/date_jisuanqi

Online calendar:
Http://tools.jb51.net/bianmin/wannianli

Online calendar/Calendar Conversion Tool:
Http://tools.jb51.net/bianmin/yinli2yangli

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.