日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Python?arrow模塊使用方法_python

作者:HHYZBC ? 更新時間: 2022-11-22 編程語言

下載安裝該模塊

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 不會隨季節變化而變化。

to方法

to 可以將一個本地時區轉換成其它任意時區

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]>

需要注意的是,如果傳入的參數是日期字符串,則需要像最后一個例子指定時間格式,否則解析結果會不準確,但是不會報錯

原文鏈接:https://blog.csdn.net/HHYZBC/article/details/127180791

欄目分類
最近更新