time & datetime模块

Posted 故穿庭树作飞花

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了time & datetime模块相关的知识,希望对你有一定的参考价值。

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模块的主要内容,如果未能解决你的问题,请参考以下文章

python常用模块之time&datetime模块

Python常用模块——time&datetime模块

8.模块介绍 time &datetime模块 random os sys shutil json & picle shelve xml处理 yaml处理 configparser h

python的time&datetime模块

python之时间模块 time & datetime & calendar简介

Python时间模块time&datetime