首页 > 其他 > 详细

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

时间:2015-06-07 14:33:17      阅读:300      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!