python获取一年所有的日期

自动识别闰年。

import arrow

def isLeapYear(years):
'''
通过判断闰年,获取年份years下一年的总天数
:param years: 年份,int
:return:days_sum,一年的总天数
'''
# 断言:年份不为整数时,抛出异常。
assert isinstance(years, int), "请输入整数年,如 2018" if ((years % 4 == 0 and years % 100 != 0) or (years % 400 == 0)): # 判断是否是闰年
# print(years, "是闰年")
days_sum = 366
return days_sum
else:
# print(years, '不是闰年')
days_sum = 365
return days_sum def getAllDayPerYear(years):
'''
获取一年的所有日期
:param years:年份
:return:全部日期列表
'''
start_date = '%s-1-1' % years
a = 0
all_date_list = []
days_sum = isLeapYear(int(years))
print()
while a < days_sum:
b = arrow.get(start_date).shift(days=a).format("YYYY-MM-DD")
a += 1
all_date_list.append(b)
# print(all_date_list)
return all_date_list if __name__ == '__main__':
# years = "2001"
# years = int(years)
# # 通过判断闰年,获取一年的总天数
# days_sum = isLeapYear(years) # 获取一年的所有日期
all_date_list = getAllDayPerYear("2000")
print(all_date_list)

  

输出:

最新文章

  1. Android Studio项目转Eclipse项目
  2. jquery 监听radio选中,取值
  3. memcached 基本操作
  4. Windows 8.1 应用开发后台任务概述(Windows XAML)
  5. Eclipse中使用Junit编写测试用例
  6. TFS 图标意思
  7. bit-map牛刀小试:数组test[X]的值所有在区间[1, 8000]中, 现要输出test中反复的数。要求:1. 不能改变原数组; 2.时间复杂度为O(X);3.除test外空间不超过1KB
  8. LeetCode OJ 75. Sort Colors
  9. ubuntu14.04英文环境下安装中文输入法
  10. 数据库及SQL----常用知识点总结
  11. Linux系统LVM基本使用
  12. ABP官方文档翻译 4.6 审计日志
  13. day09内存管理
  14. 正则表达式matcher.group用法
  15. ES5数组的遍历方式
  16. Linux期中总结
  17. SpringMVC跨重定向请求传递数据
  18. 证明 U and V={0}时 dim(U+V)=dim(U)+dim(V)
  19. php 二维数组打乱顺序
  20. secureCRT连不上虚拟机上的ubuntu

热门文章

  1. SNF快速开发平台项目实践介绍
  2. Delphi如何处理在进行大量循环时,导致的应用程序没有响应的情况
  3. 解決中文地址Uri.IsWellFormedUriString返回false
  4. 训练的时候 nvidia:自动刷新
  5. 猿题库从 Objective-C 到 Swift 的迁移
  6. 【转载】基于rasa的对话系统搭建(上)
  7. HTTP 05 安全
  8. 将RAC软件转换为单实例软件
  9. 4 playlook-Jinja2 filter
  10. java生成txt文件,读txt文件