1. 程式人生 > >time & datetime模塊

time & datetime模塊

get date %d 修改 結果 print ctime tuple pri

import time
import datetime
import os

#獲取當前時間戳
time.time()

#將時間戳轉換為struct_time 時間
print(time.localtime())
print(time.localtime(1513651681))

#將struct_time 時間轉換成時間戳
print(time.mktime(time.localtime()))

#將struct_time 時間轉換成字符串
print(time.strftime(‘%Y-%m-%d %H:%M:%S‘,time.localtime()))

#將字符串轉換成struct_time 時間
print(time.strptime(‘2017-10-10 15:13:12‘,‘%Y-%m-%d %H:%M:%S‘))

#獲取文件創建時間
print(os.path.getctime(‘deg.py‘))      #得到時間戳
print(time.localtime(os.path.getctime(‘deg.py‘)))  #時間戳轉成struct_time
print(time.strftime(‘%Y-%m-%d %H:%M:%S‘,time.localtime(os.path.getctime(‘deg.py‘))))   #時間戳轉成struct_time 在轉成字符串

#獲取文件訪問時間
print(os.path.getatime(‘deg.py‘))

#獲取文件修改時間
print(os.path.getmtime(‘deg.py‘))

#得到當前日期
print(datetime.date.today(),type(datetime.date.today()))  # 日期格式datetime.date

#得到當前時間

print(datetime.datetime.now(),type(datetime.datetime.now()))  # 日期時間格式

#得到當前時間struct_time 格式
print(datetime.datetime.now().timetuple(),type(datetime.datetime.now().timetuple()))  # struct_time 格式

#得到當前時間struct_time 格式轉化成時間戳
print(time.mktime(datetime.datetime(2017,12,12,11,26,26).timetuple()))

#時間戳轉換為日期
a=time.mktime(datetime.datetime(2017,12,12,11,26,26).timetuple())
datetime.datetime.fromtimestamp(a)    #datetime.datetime(2017, 12, 12, 11, 26, 26)

#指定日期
datetime.date(2017,10,10)

#指定日期時間

datetime.datetime(2017,10,10,12,13,16)

#日期轉換為字符串
datetime.datetime(2017,10,10,12,13,16).strftime(‘%Y-%m-%d %H:%M:%S‘)   #‘2017-10-10 12:13:16‘

#字符串轉換為日期
datetime.datetime.strptime(‘2017-10-10 12:13:16‘,‘%Y-%m-%d %H:%M:%S‘)   #datetime.datetime(2017, 10, 10, 12, 13, 16)

#datetime.timedelta

#前一小時

#前一天

#後三天

datetime.datetime.now() - datetime.timedelta(hours = 1)
#datetime.datetime(2017, 12, 19, 11, 55, 41, 284443)
datetime.datetime.now() - datetime.timedelta(hours = -1)
#datetime.datetime(2017, 12, 19, 13, 56, 35, 542420)

datetime.datetime.now() - datetime.timedelta(days = -1)
#datetime.datetime(2017, 12, 20, 12, 56, 44, 944484)

datetime.datetime.now() - datetime.timedelta(days = 1)
#datetime.datetime(2017, 12, 18, 12, 56, 49, 300419)

datetime.datetime.now() - datetime.timedelta(minutes = 10)
#datetime.datetime(2017, 12, 19, 12, 47, 5, 457422)

結果:

time.struct_time(tm_year=2017, tm_mon=12, tm_mday=19, tm_hour=12, tm_min=58, tm_sec=32, tm_wday=1, tm_yday=353, tm_isdst=0)
time.struct_time(tm_year=2017, tm_mon=12, tm_mday=19, tm_hour=10, tm_min=48, tm_sec=1, tm_wday=1, tm_yday=353, tm_isdst=0)
1513659512.0
2017-12-19 12:58:32
time.struct_time(tm_year=2017, tm_mon=10, tm_mday=10, tm_hour=15, tm_min=13, tm_sec=12, tm_wday=1, tm_yday=283, tm_isdst=-1)
1511856946.5942597
time.struct_time(tm_year=2017, tm_mon=11, tm_mday=28, tm_hour=16, tm_min=15, tm_sec=46, tm_wday=1, tm_yday=332, tm_isdst=0)
2017-11-28 16:15:46
1511857233.4906693
1511857233.4906693
2017-12-19 <class ‘datetime.date‘>
2017-12-19 12:58:32.463455 <class ‘datetime.datetime‘>
time.struct_time(tm_year=2017, tm_mon=12, tm_mday=19, tm_hour=12, tm_min=58, tm_sec=32, tm_wday=1, tm_yday=353, tm_isdst=-1) <class ‘time.struct_time‘>
1513049186.0

time & datetime模塊