網站首頁 編程語言 正文
下載安裝該模塊
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
相關推薦
- 2022-05-23 Python+OpenCV實現在圖像上繪制矩形_python
- 2023-01-18 GO實現跳躍表的示例詳解_Golang
- 2022-09-25 Django 遷移錯誤 Cannot add foreign key constraint,字段類型
- 2022-11-16 通用?HTTP?簽名組件的另類實現方式_實用技巧
- 2022-09-02 Python常用編碼的區別介紹_python
- 2022-07-03 C語言數組全面詳細講解_C 語言
- 2022-12-27 詳解PyQt5?事件處理機制_python
- 2023-05-08 C語言中隊列的結構和函數接口的使用示例_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支