神剑山庄资源网 Design By www.hcban.com
在获取贝壳分的时候用到了时间处理函数,想要获取上个月时间包括年、月、日等
# 方法一: today = datetime.date.today() # 1. 获取「今天」 first = today.replace(day=1) # 2. 获取当前月的第一天 last_month = first - datetime.timedelta(days=1) # 3. 减一天,得到上个月的最后一天 print(last_month.strftime("%Y%m")) # 4. 格式化成指定形式 # 方法二: today = datetime.date.today() # 1. 获取「今天」 last_month = today.replace(month=today.month - 1) # 2.获取前一个月 print(last_month.strftime("%Y%m")) # 3. 格式化成指定形式 # 方法三: arrow包的使用(pip install arrow) a = arrow.now() # 当前本地时间 print(a.timestamp) print(a.year) print(a.month) print(a.day) print(a.date()) print(a.time()) print(a.shift(months=-4).format("YYYYMM")) print(a.shift(months=1).format("YYYYMM")) print(a.shift(hours=1)) # 生成arrow对象 print(arrow.get(1535113845)) print(arrow.get(datetime.date(2018, 7, 24))) print(arrow.get("2018-08-11 12:30:56"))
运行结果如下:
# 方法一
201906
# 方法二
201906
# 方法三
1562329178
2019
7
5
2019-07-05
20:19:38.573000
201903
201908
2019-07-05T21:19:38.573000+08:00
2018-08-24T12:30:45+00:00
2018-07-24T00:00:00+00:00
2018-08-11T12:30:56+00:00
所以想通过一个方法来兼容n种情况是极度困难的,内部实现也会非常复杂,作为用户使用起来必然也很混乱,我们需要根据自己的业务场景选取最合适的包来进行处理。
总结
神剑山庄资源网 Design By www.hcban.com
神剑山庄资源网
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
神剑山庄资源网 Design By www.hcban.com
暂无关于Python 中的时间处理包datetime和arrow的方法详解的评论...
更新日志
2024年09月30日
2024年09月30日
- 孙悦.1996-伙伴【正大国际】【WAV+CUE】
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[FLAC/分轨][399.62MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[320K/MP3][87.4MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[FLAC/分轨][184.94MB]
- 陈慧娴.2018-Priscilla-Ism演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郑秀文.1999-我应该得到(国)【华纳】【WAV+CUE】
- 陈家慧.2011-钢琴酒吧2CD【龙吟唱片】【WAV+CUE】
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[320K/MP3][45.01MB]
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[FLAC/分轨][109.13MB]
- 赞多《序章》[320K/MP3][45.54MB]
- 许巍.2004-每一刻都是崭新的【步升大风】【WAV+CUE】
- 群星.2024-四方馆影视原声带【韶愔音乐】【FLAC分轨】
- 陈雷.1997-安锁咧【金圆唱片】【WAV+CUE】
- 关淑怡.2013-MY.FAVORITE.SK.3CD【环球】【WAV+CUE】
- Sweety.2006-花言乔语【丰华】【WAV+CUE】