def is_leap_year(year): # 判断闰年,是则返回True,否则返回False
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
return True
else:
return False
def function1(year, month, day): # 计算给定日期是那一年的第几天
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
import datetime
import time
def function2(year, month, day): # 直接使用Python内置模块datetime的格式转换功能得到结果
date = datetime.date(year, month, day)
return date.strftime('%j')
# 假如输入格式为字符串(比如从命令行读入字符串2016-10-1),则需要先对输入内容进行处理
_input = '2016-10-1'
_year1 = int(_input.split('-')[0])
_month1 = int(_input.split('-')[1])
_day1 = int(_input.split('-')[2])
# 当然你也可以用datetime的内置方法进行格式处理
t = time.strptime(_input, '%Y-%m-%d')
_year2 = t.tm_year
_month2 = t.tm_mon
_day2 = t.tm_mday
import datetime
import time
def is_leap_year(year): # 判断闰年,是则返回True,否则返回False
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
return True
else:
return False
def function1(year, month, day): # 计算给定日期是那一年的第几天
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): # 直接使用Python内置模块datetime的格式转换功能得到结果
date = datetime.date(year, month, day)
return date.strftime('%j')
print(function1(2016, 10, 1))
print(function2(2016, 10, 1))
# 假如输入格式为字符串(比如从命令行读入字符串2016-10-1),则需要先对输入内容进行处理
_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))
# 当然你也可以用datetime的内置方法进行格式处理
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))
# 后面发现我为了编函数写复杂了,如果输入是字符串其实一句话就好
import time
_input = '2016-10-1'
# 详见Python日期和字符串格式互相转换 http://www.1sucai.cn/article/66019.htm
t = time.strptime(_input, '%Y-%m-%d')
print(time.strftime('%j',t))
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有