Question Fourth: Enter a certain date of the year, Judge this day is the first days of the year?

Source: Internet
Author: User
#输入某年某月某日, judge that day is the first of the year.
"' Thinking: First, the date is divided into the input or a piece of input, think of if a piece of input may also need to split to find the date
So it is better to divide the input; then consider the number of days that are in fact the number of days of the month and the number of days of the month (such as: July 5 is the first 6-month days plus 5)
So the next question is how to find the sum of the days of the first few months, where we use a list to store the number of days in the first few months,
LIST=[0,31,59,90,120,151,181,212,243,273,304,334,365], then July 5 is the first few days can be simply expressed: 5+list[month-1].
Also need to consider is leap year or excepting, leap year February is 29 days, leap year judgment condition: (year%4==0 and year%100!=0) | | Year%400==0 ' "

#必须首先转化成整型 because input () defaults to character type
#and和or的使用
#输入时合法的判断

#方法一: First verify that the date entered is correct, the direct output of a list of conditions, is a leap year and then add a
'''
Year=int (Input (' Input year: ') #首先判断: Whether the date entered is legal
if (year>0):
Month=int (Input (' Input month: '))
if (0<month<13):
Day=int (Input (' Input day: '))
DAYLIST=[31,29,31,30,31,30,31,31,30,31,30,31] #daylist为限制天数做准备, the number of days per month is a range
if (0<day<=daylist[month-1]):
Days=0
LIST=[0,31,59,90,120,151,181,212,243,273,304,334,365]
if (year% 4 = 0 and year%100!=0 and month>2) or (Year%400==0 and month>2): #闰年的二月是特殊的, one less condition: four years and a century does not leap or 400 years, There are also months to be greater than 2, between and OR
Days=day+list[month-1]+1
Else
DAYS=DAY+LIST[MONTH-1]
Print (days)
Else
Print (' Day error ')
Else
Print (' month error ')
Else
Print (' year error ')
'''
#方法二: The whole input month day, with the segmentation method to find the date of the split (), so that can be connected to the input date
'''
Date=input (' Enter month Day: yyyy-mm-dd: ')
y,m,d= (int (i) for I in Date.split ('-'))
Dth=0
MONTHS1=[0,31,60,91,121,152,182,213,244,274,305,335,366] #闰年
MONTHS2=[0,31,59,90,120,151,181,212,243,273,304,334,365] #平年
if ((y%4==0) and (y%100!=0)) or (y%100==0) and (y%400==0):
Dth=months1[m-1]+d
Else
Dth=months2[m-1]+d
Print (' Total days: ', Dth)
'''
#方法三: With a leap sign
"Year=int" (Input (' Input year: '))
Month=int (Input (' Input month: '))
Day=int (Input (' Input day: '))
Days=0
LIST=[0,31,59,90,120,151,181,212,243,273,304,334,365]
if (0<month<13):
Days=list[month-1]+day
Else
Print (' Data error ')
Leap=0
if (year% 4 = 0 and year%100!=0 and month>2) or (Year%400==0 and month>2): #闰年的二月是特殊的, one less condition: four years and a century does not leap or 400 years, There are also months to be greater than 2, between and OR
Leap=1
if (leap==1):
Print (days+1)
Else
Print (days)
'''
#方法四: Two list, judging is leap year or excepting, directly corresponding to the list on it
"" while (1):
Year=int (Input ("Year: \ n"))
Month=int (Input ("Month: \ n"))
Day=int (Input ("Day: \ n"))
MONTHS1=[0,31,60,91,121,152,182,213,244,274,305,335,366] #闰年
MONTHS2=[0,31,59,90,120,151,181,212,243,273,304,334,365] #平年
if ((year%4==0) and (year%100!=0)) or (year%100==0) and (year%400==0):
Dth=months1[month-1]+day
Else
Dth=months2[month-1]+day
Print (' Total days: ', Dth)
'''
#方法五: Using the time module
'''
Import time
Print (Time.strptime (' 2017-9-20 ', '%y-%m-%d ') [7]) #此处的大小写必须是这样的
'''
#方法六: Using the Calendar module
#!/usr/bin/env python
#-*-Coding:utf-8-*-
'''
Import Calendar
year = Int (input ("Year:"))
month = Int (input ("month:"))
day = Int (input (' Day: '))
Days = 0
If 0 < month <= 12:
_, Month_day = Calendar.monthrange (year, month)
If Day <= month_day:
For M in range (1, month):
_, Month_day = Calendar.monthrange (year, m)
Days = Month_day
Days = Day
Print (days)
Else
Print ("Input day Error")
Else
Print ("Input month Error")
'''
#方法七: Import Time Module
#! /usr/bin/env python
# Coding:utf-8

Import time
D=input ("Please enter the year, format such as xxxx-xx-xx:")
D=time.strptime (D, '%y-%m-%d '). Tm_yday
Print ("The {} of this year!". Format (d))





































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.