码迷,mamicode.com
首页 > 其他好文 > 详细

Practice:输入年月日,判断为一年的第几天

时间:2015-06-07 14:35:19      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

#-*- coding:utf-8 -*-
‘‘‘
Created on 2015-6-7
# 输入年月日,判断为一年的第几天
@author: AdministrInputator
‘‘‘
# strInput = ‘150223‘
# a = int(strInput[-2:])
# print(a)
def leapYear(year): # 判断平闰年,由于输入年份只有两位数,‘00’~‘69’转换为2000~2069,‘70’~’99‘转换为1970~1999
if year < 70:
year += 2000
else:
year += 1900
if year % 4 == 0 and year % 400 != 0:
return True
else:
return False

def dateJudge(strInput): # 计算当日在一年中第几天
year = int(strInput[:2])
month = int(strInput[2:4])
day = int(strInput[-2:])
# print(day)
theDay = 0
# if len(strInput) != 6:
# print(‘请输入六位数字!‘)
# elif month > 12:
# print(‘月份不能大于 12‘)

daysLeapYear = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] # 闰年所有月份的天数
daysNonLeapYear = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if leapYear(year):
for index in range(0, month - 1):
theDay += daysLeapYear[index]
else:
for index in range(0, month - 1):
theDay += daysNonLeapYear[index]
theDay += day # 加上天数
return theDay
print(dateJudge(‘150607‘))

Practice:输入年月日,判断为一年的第几天

标签:

原文地址:http://www.cnblogs.com/peiqianggao/p/4558403.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!