網(wǎng)站首頁 編程語言 正文
下載安裝該模塊
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 用于航空,天氣預報,飛行計劃,空中交通管制通關和映射。 與當?shù)貢r間不同,UTC 不會隨季節(jié)變化而變化。
to方法
to 可以將一個本地時區(qū)轉換成其它任意時區(qū)
arrow.now() // 獲取當前時間
arrow.now().to("utc") // 將當前時間轉為utc時間
arrow.now().to("utc").to("local") // 將轉換后的utc時間再轉為當?shù)貢r間
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) # 減一星期
注意參數(shù)后面都有一個s,其他的同理
humanize方法
獲取人性化的日期和時間,比如一個小時前、5分鐘前。默認是英文格式,指定 locale 可顯示相應的語言格式。
a.shift(hours=1).humanize()
'1 hours ago'
a.shift(hours=1).humanize(locale='zh')
'1小時前'
format方法
格式化時間,可以根據(jù)指定的格式將 arrow 對象轉換成字符串格式
get()方法
用于解析時間。
# 不帶參數(shù),等價與 utcnow()
>>> arrow.get()
<Arrow [2018-08-24T07:11:50.528742+00:00]>
# 接受時間戳參數(shù)
>>> 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]>
需要注意的是,如果傳入的參數(shù)是日期字符串,則需要像最后一個例子指定時間格式,否則解析結果會不準確,但是不會報錯
原文鏈接:https://blog.csdn.net/HHYZBC/article/details/127180791
相關推薦
- 2022-12-27 Python中的類的定義和對象的創(chuàng)建方法_python
- 2022-11-17 WPF利用DrawingContext實現(xiàn)繪制溫度計_C#教程
- 2022-11-18 React生命周期函數(shù)深入全面介紹_React
- 2022-09-08 pytest實現(xiàn)多進程與多線程運行超好用的插件_python
- 2022-11-20 Pandas數(shù)據(jù)處理庫畫圖與文件讀取使用示例_python
- 2022-12-30 解決React報錯The?tag?is?unrecognized?in?this?browser_R
- 2022-08-10 pandas.DataFrame.from_dict直接從字典構建DataFrame的方法_pyth
- 2022-03-19 Docker匿名掛載和具名掛載的具體使用_docker
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支