Python得到前面12个月的数据,Python得到现在时间 前一年的数据,
#Python 实现得到现在时间12个月前的每个月 # 假设现在的时间是2016年9月25日 #得到现在的时间 得到now等于2016年9月25日 now = datetime.datetime.now() #得到今年的的时间 (年份) 得到的today_year等于2016年 today_year = now.year #今年的时间减去1,得到去年的时间。last_year等于2015 last_year = int(now.year) -1 #得到今年的每个月的时间。today_year_months等于1 2 3 4 5 6 7 8 9, today_year_months = range(1,now.month+1) #得到去年的每个月的时间 last_year_months 等于10 11 12 last_year_months = range(now.month+1, 13) #定义列表去年的数据 data_list_lasts = [] #通过for循环,得到去年的时间夹月份的列表 #先遍历去年每个月的列表 for last_year_month in last_year_months: # 定义date_list 去年加上去年的每个月 date_list = '%s-%s' % (last_year, last_year_month) #通过函数append,得到去年的列表 data_list_lasts.append(date_list) data_list_todays = [] #通过for循环,得到今年的时间夹月份的列表 #先遍历今年每个月的列表 for today_year_month in today_year_months: # 定义date_list 去年加上今年的每个月 data_list = '%s-%s' % (today_year, today_year_month) #通过函数append,得到今年的列表 data_list_todays.append(data_list) #去年的时间数据加上今年的时间数据得到年月时间列表 data_year_month = data_list_lasts + data_list_todays data_year_month.reverse()
程序运行过程图片
如果觉得对您有帮助,麻烦您点一下推荐,谢谢!
好记忆不如烂笔头
好记忆不如烂笔头
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术