目錄
1,匯入時間模組包
2,獲取當前時間的時間元組
3,將時間元組轉成時間戳
4,求三天前的時間戳
5,將上述時間戳轉換成時間元組
6,格式化時間
python中時間日期格式化符號:
7,列印指定的時間
時間元組的屬性如下:
import time;
列印如下tupletime = time.localtime();
print(time.localtime());
time.struct_time(tm_year=2018, tm_mon=10, tm_mday=3, tm_hour=16, tm_min=48, tm_sec=22, tm_wday=2, tm_yday=276, tm_isdst=0)
另外 localtime中引數,接受時間戳。
列印如下mktime = time.mktime(tupletime);
print(mktime);
1538556502.0
列印如下threeday = mktime - 60*60*24*3;
print(threeday);
1538297302.0
列印如下:threeday_localtime = time.localtime(threeday);
print(threeday_localtime);
time.struct_time(tm_year=2018, tm_mon=9, tm_mday=30, tm_hour=16, tm_min=48, tm_sec=22, tm_wday=6, tm_yday=273, tm_isdst=0)
列印如下:threedays = time.strftime('%y-%m-%d %h:%m:%s',threeday_localtime);
print(threedays);
注意:
格式化時間中,不接受中文,否則會報錯。如下圖所示
print(str(timestrp.tm_year)+'年'+str(timestrp.tm_mon)+'月'+str(timestrp.tm_mday)+'日'+str(timestrp.tm_hour)+'時'+str(timestrp.tm_min)+'分'+str(timestrp.tm_sec)+'秒');
列印如下:
2023年8月8日8時8分8秒
![](https://pic.w3help.cc/e4b/3af95cacb975e8953a0352ab86db4.jpeg)
python時間模組的使用
前言 在開發中經常會與時間打交道,如 獲取事件戳,時間戳的格式化等,這裡簡要記錄一下python操作時間的方法。python中常見的處理時間的模組 time模組介紹 說明 time模組主要講解如下內容 1.時間戳 時間元組格式 time.struct time 日期字串 import time ti...
python 時間模組
import os import time s 2019 7 14 print time.strptime s,y m d s time 09 00 00 print time.strptime s time,h m s 把元組的時間轉換為時間戳 tuple time time.localtime ...
python 時間模組
格式化時間字串 y 兩位數的年份表示 00 99 y 四位數的年份表示 0000 9999 m 月份 01 12 d 月內的一天 0 31 h 24小時制的小時數 0 23 i 12小時制的小時數 01 12 m 分鐘數 00 59 s 秒 00 59 a 本地簡化星期名稱 a 本地完整星期名稱 b...