網站首頁 編程語言 正文
獲取當前系統的日期
python獲取當前系統時間,主要通過Python中的datetime模塊來實現。
import datetime from time import strftime
獲取當前時間
now=datetime.datetime.now() print(now.strftime("%Y-%m-%d %H:%M:%S"))
獲取當前時間+3分鐘之后的時間:
upperTime=(datetime.datetime.now()+datetime.timedelta(minutes=3)).strftime("%Y-%m-%d %H:%M:%S") print(upperTime)
獲取當前時間+5分鐘之后的時間:
upperTime=(datetime.datetime.now()+datetime.timedelta(minutes=5)).strftime("%Y-%m-%d %H:%M:%S") print(upperTime)
分別打印結果為:
2022-04-16 07:47:54
2022-04-16 07:50:54
2022-04-16 07:52:54
-
timedelta-
時間間隔,即兩個時間點之間的長度 -
now(tz=None)
返回當前的本地日期和時間。如果可選參數tz沒有指定,與today()一樣。 -
strftime(format)
返回一個表示日期的字符串,由顯式格式字符串控制。引用小時、分鐘或秒的格式代碼將看到0值。
datetime包獲得當前日期時間
在python中使用datetime包獲取當前日期時間的方法。
datetime類
datetime是python內置的一個組件包,datetime包中有一個datetime類,還有一個now()方法用于獲取當前的時間的datetime對象:
import datetime now = datetime.datetime.now() print(now) # 2022-03-20 18:32:14.178030
datetime類有year, month, day, hour, minute, second, microsecond等成員,比如上面的對象now:
# now: datetime.datetime(2022, 3, 20, 18, 32, 14, 178030) print(now.year) # 2022 print(now.month) # 3 print(now.day) # 20
strftime按指定格式輸出字符
datetime類的方法strftime,按指定格式從datetime對象返回一個字符串(不會改變對象自身):
s = now.strftime('%y%m%d') print(s) # '220320' print(now) # 2022-03-20 18:32:14.178030 # now: datetime.datetime(2022, 3, 20, 18, 32, 14, 178030)
其中,常用的格式表示如下:
-
%y
兩位數的年份表示(00-99) -
%Y
四位數的年份表示(000-9999) -
%m
月份(01-12) -
%d
月內中的一天(0-31) -
%H
24小時制小時(0-23) -
%M
分鐘(00-59) -
%S
秒(00-59)
原文鏈接:https://blog.csdn.net/YZL40514131/article/details/124207896
相關推薦
- 2021-11-26 Linux下查看IP地址不顯示解決辦法_Linux
- 2022-11-10 使用python的pandas讀取excel文件中的數據詳情_python
- 2022-10-17 Net?core中使用System.Drawing對上傳的圖片流進行壓縮(示例代碼)_實用技巧
- 2022-08-22 Python中可以用三種方法判斷文件是否存在_python
- 2022-09-27 詳解adb工具的基本使用_Android
- 2022-10-27 Python數值求解微分方程方法(歐拉法,隱式歐拉)_python
- 2022-11-13 Python?pandas按行、按列遍歷DataFrame的幾種方式_python
- 2022-07-30 windows安裝matplotlib方法(cmd+pycharm)+cmd不運行python命令解
- 最近更新
-
- 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同步修改后的遠程分支