關(guān)于Python 中的時間處理包datetime和arrow的方法詳解
在獲取貝殼分的時候用到了時間處理函數(shù),想要獲取上個月時間包括年、月、日等
# 方法一:today = datetime.date.today() # 1. 獲取「今天」first = today.replace(day=1) # 2. 獲取當(dāng)前月的第一天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() # 當(dāng)前本地時間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'))
運行結(jié)果如下:
# 方法一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種情況是極度困難的,內(nèi)部實現(xiàn)也會非常復(fù)雜,作為用戶使用起來必然也很混亂,我們需要根據(jù)自己的業(yè)務(wù)場景選取最合適的包來進(jìn)行處理。
總結(jié)
到此這篇關(guān)于關(guān)于Python 中的時間處理包datetime和arrow的方法詳解的文章就介紹到這了,更多相關(guān)python 時間處理包datetime和arrow內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Nginx+php配置文件及原理解析2. Intellij IDEA 2019 最新亂碼問題及解決必殺技(必看篇)3. Android自定義View實現(xiàn)掃描效果4. java中throws實例用法詳解5. Opencv+Python識別PCB板圖片的步驟6. CSS3實現(xiàn)動態(tài)翻牌效果 仿百度貼吧3D翻牌一次動畫特效7. IOS利用CocoaHttpServer搭建手機(jī)本地服務(wù)器8. Android Manifest中meta-data擴(kuò)展元素數(shù)據(jù)的配置與獲取方式9. css3溢出隱藏的方法10. ASP.NET MVC獲取多級類別組合下的產(chǎn)品
