久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

關于Python 中的時間處理包datetime和arrow的方法詳解

瀏覽:123日期:2022-08-01 18:20:21

在獲取貝殼分的時候用到了時間處理函數,想要獲取上個月時間包括年、月、日等

# 方法一: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# 方法三15623291782019752019-07-0520:19:38.5730002019032019082019-07-05T21:19:38.573000+08:002018-08-24T12:30:45+00:002018-07-24T00:00:00+00:002018-08-11T12:30:56+00:00

所以想通過一個方法來兼容n種情況是極度困難的,內部實現也會非常復雜,作為用戶使用起來必然也很混亂,我們需要根據自己的業務場景選取最合適的包來進行處理。

總結

到此這篇關于關于Python 中的時間處理包datetime和arrow的方法詳解的文章就介紹到這了,更多相關python 時間處理包datetime和arrow內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 云霄县| 比如县| 囊谦县| 即墨市| 宜兰县| 久治县| 富平县| 东乡县| 新沂市| 弥勒县| 延寿县| 和平区| 余江县| 藁城市| 阳新县| 保德县| 肇州县| 潮安县| 金沙县| 屏边| 库伦旗| 庄河市| 东海县| 苏尼特左旗| 大足县| 龙陵县| 汝南县| 上杭县| 盘锦市| 观塘区| 双江| 正阳县| 长垣县| 怀柔区| 蓬安县| 舒城县| 正宁县| 许昌市| 大竹县| 饶阳县| 德兴市|