Python arrow模塊使用方法
下載安裝該模塊
pip install arrow
基本使用
a = arrow.now() # 當前本地時間
arrow.utcnow() # 當前utc時間
a.datetime # 獲取datetime對象
a.timestamp # 獲取時間戳
a.year # 獲取年
a.month # 獲取月
a.day # 獲取日
a.hour # 獲取時
a.date() # 獲取年月日
a.time() # 獲取時分秒
UTC(世界標準時間)是主要時間標準。 UTC 用于航空,天氣預報,飛行計劃,空中交通管制通關和映射。 與當地時間不同,UTC 不會隨季節(jié)變化而變化。
to方法
to 可以將一個本地時區(qū)轉換成其它任意時區(qū)
arrow.now() // 獲取當前時間 arrow.now().to("utc") // 將當前時間轉為utc時間 arrow.now().to("utc").to("local") // 將轉換后的utc時間再轉為當地時間 arrow.now().to("America/New_York") // 將時間轉為紐約時間 arrow.now().to('US/Pacific') arrow.now().to('Europe/Bratislava') arrow.now().to('Europe/Moscow')
shift方法
shift 有點像游標卡尺,可以左右兩邊進行加減移位操作,加減的對象可以是年月日時分秒和星期
a.shift(months=-1) # 減一個月時間 a.shift(months=1) # 加一個月時間 a.shift(years=-2) # 減兩年時間 a.shift(hours=1) # 加一小時 a.shift(weeks=1) # 減一星期
注意參數后面都有一個s,其他的同理
humanize方法
獲取人性化的日期和時間,比如一個小時前、5分鐘前。默認是英文格式,指定 locale 可顯示相應的語言格式。
a.shift(hours=1).humanize() '1 hours ago' a.shift(hours=1).humanize(locale='zh') '1小時前'
format方法
格式化時間,可以根據指定的格式將 arrow 對象轉換成字符串格式
get()方法
用于解析時間。
# 不帶參數,等價與 utcnow()
>>> arrow.get()
<Arrow [2018-08-24T07:11:50.528742+00:00]>
# 接受時間戳參數
>>> arrow.get(1535113845)
# 接受一個datetime對象
>>> arrow.get(datetime(2018,8,24))
<Arrow [2018-08-24T00:00:00+00:00]>
# 接收一個date對象
>>> from datetime import date
>>> arrow.get(date(2018,7,24))
<Arrow [2018-07-24T00:00:00+00:00]>
# 接收日期格式的字符串
>>> arrow.get("2018-08-11 12:30:56")
<Arrow [2018-08-11T12:30:56+00:00]>
# 接收日期字符串,并指定格式
>>> arrow.get("18-08-11 12:30:56", "YY-MM-DD HH:mm:ss")
<Arrow [2018-08-11T12:30:56+00:00]>
需要注意的是,如果傳入的參數是日期字符串,則需要像最后一個例子指定時間格式,否則解析結果會不準確,但是不會報錯
到此這篇關于Python arrow模塊使用方法的文章就介紹到這了,更多相關Python arrow內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
使用pandas實現csv/excel sheet互相轉換的方法
今天小編就為大家分享一篇使用pandas實現csv/excel sheet互相轉換的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12