python datetime模組操作

2021-10-23 09:39:07 字數 1040 閱讀 3216

開發過程中通常會使用datetime,精確到某一位的情況

# conding=utf8

from datetime import datetime, timedelta

import time

a = datetime.now(

)print

(a.strftime(

"%y-%m-%d %h:%m:%s"))

# 獲取當前時間精確到秒數

# 2018-01-28 23:33:18

print

(a.strftime(

"%y-%m-%d %h:%m"))

# 獲取當前時間精確到分鐘

# 2018-01-28 23:33

print

(a.strftime(

"%y-%m-%d %h"))

# 獲取當前時間精確到小時

# 2018-01-28 23

print

(a.strftime(

"%y-%m-%d"))

# 獲取當前時間精確帶天

# 2018-01-28

print

((a - timedelta(seconds=5)

).strftime(

"%y-%m-%d %h:%m:%s"))

# 2018-01-28 23:36:39

print

((a - timedelta(minutes=5)

).strftime(

"%y-%m-%d %h:%m"))

# 2018-01-28 23:28

print

((a - timedelta(hours=5)

).strftime(

"%y-%m-%d %h"))

# 2018-01-28 18

print

((a - timedelta(days=5)

).strftime(

"%y-%m-%d"))

# 2018-01-23

python datetime處理時間

datetime date time都提供了strftime 方法,該方法接收乙個格式字串,輸出日期時間的字串表示。下表是從python手冊中拉過來的,我對些進行了簡單的翻譯 翻譯的有點噢口 格式字元 意義 a 星期的簡寫。如 星期三為web a星期的全寫。如 星期三為wednesday b月份的簡...

python datetime學習筆記

import datetime d datetime.date 2016,7,24 新建乙個日期,注意前面的0不能有 print d 20106 07 24 d datetime.date.today 獲取當前日期 print d print d.year,d.month,d.day print d...

Python datetime模組小結

模組小結如下 另外有time模組小結 from datetime import date,time,datetime,timedelta datetime.now 當前日期元組 datetime.today 當前日期元組 d1 2018 05 09 10 20 15 d2 2018 06 02 08...