码迷,mamicode.com
首页 > 其他好文 > 详细

time & datetime模块

时间:2017-12-19 15:06:30      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:etc   ftime   文件   time   print   datetime   获取文件   格式转化   time()   

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模块

标签:etc   ftime   文件   time   print   datetime   获取文件   格式转化   time()   

原文地址:http://www.cnblogs.com/mao3714/p/8065020.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!