python 時間模組備忘

2021-09-03 08:31:32 字數 1536 閱讀 4632

由於要用到時間模組,為了下次不用去翻文件,這裡也簡單記錄一下:

直接乙個指令碼看輸出:

import time

print time.time()

print time.localtime(time.time())

print time.strftime('%y-%m-%d', time.localtime())

print time.strftime('%y-%m-%d', time.localtime())

print time.strftime('%y-%m-%d %h:%m:%s', time.localtime())

print time.strftime('%y-%m-%d %i:%m:%s', time.localtime())

print time.strftime('%y-%m-%d %h:%m:%s --%a--%c', time.localtime())

print time.strftime('%y-%m-%d %h:%m:%s --%x--%x', time.localtime())

檢視結果:

[root@www python]# python time1.py 

1425623399.84

time.struct_time(tm_year=2015, tm_mon=3, tm_mday=6, tm_hour=14, tm_min=29, tm_sec=59, tm_wday=4, tm_yday=65, tm_isdst=0)

2015-03-06

15-03-06

2015-03-06 14:29:59

2015-03-06 02:29:59

2015-03-06 14:29:59 --friday--fri mar  6 14:29:59 2015

2015-03-06 14:29:59 --03/06/15--14:29:59

datetime模組定義了下面這幾個類:

datetime.date:表示日期的類。常用的屬性有year, month, day;

datetime.time:表示時間的類。常用的屬性有hour, minute, second, microsecond;

datetime.datetime:表示日期時間。

datetime.timedelta:表示時間間隔,即兩個時間點之間的長度。

>>> from datetime import *

>>> print datetime.today()

2015-03-06 14:43:46.870936

>>> print datetime.now()  

2015-03-06 14:43:51.313098

需求:檢視100天前是幾月幾號:

import datetime

(datetime.datetime.now() - datetime.timedelta(days = 100)).strftime("%y-%m-%d")

python 時間模組備忘

由於要用到時間模組,為了下次不用去翻文件,這裡也簡單記錄一下 直接乙個指令碼看輸出 import time print time.time print time.localtime time.time print time.strftime y m d time.localtime print 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...