python时间模块-time和datetime
2018-08-22 18:28
701 查看
python 中时间表示方法有:时间戳,即从1975年1月1日00:00:00到现在的秒数;格式化后的时间字符串;时间struct_time 元组。
struct_time元组中元素主要包括tm_year(年)、tm_mon(月)、tm_mday(日)、tm_hour(时)、tm_min(分)、tm_sec(秒)、tm_wday(weekday0 - 6(0表示周日))、tm_yday(一年中的第几天1 - 366)、tm_isdst(是否是夏令时
1. time
常用函数
time.time() 返回当前时间戳
>>> time.time() 1465370844.096474
time.ctime() 返回这种格式的时间字符’Wed Jun 8 15:27:48 2016’,显示当前时间.同时也可以转换时间戳
>>> time.ctime() Wed Aug 22 17:47:31 2018' >>> time.ctime(time.time()-86400) 'Tue Aug 21 17:50:07 2018'
time.localtime将时间戳转换成struct_time格式
>>> time.localtime() time.struct_time(tm_year=2018, tm_mon=8, tm_mday=22, tm_hour=17, tm_min=47, tm_sec=44, tm_wday=2, tm_yday=234, tm_isdst=0)
time.mktime 将struct_time格式转回成时间戳
>>> now = time.localtime() >>> now time.struct_time(tm_year=2018, tm_mon=8, tm_mday=22, tm_hour=17, tm_min=54, tm_sec=51, tm_wday=2, tm_yday=234, tm_isdst=0) >>> time.mktime(now) 1534931691.0
time.strftime 将struct_time格式转成指定的字符串格式
>>> now = time.localtime() >>> now time.struct_time(tm_year=2018, tm_mon=8, tm_mday=22, tm_hour=18, tm_min=0, tm_sec=20, tm_wday=2, tm_yday=234, tm_isdst=0) >>> time.strftime("%Y-%m-%d %H:%M:%S",now) '2018-08-22 18:00:20'
time.strptime 将自定义时间格式的字符串转换为struct_time格式
>>> time.strptime("2018-08-22","%Y-%m-%d") time.struct_time(tm_year=2018, tm_mon=8, tm_mday=22, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=234, tm_isdst=-1) >>> time.strptime("2018-08-22 18:03:30", "%Y-%m-%d %H:%M:%S") time.struct_time(tm_year=2018, tm_mon=8, tm_mday=22, tm_hour=18, tm_min=3, tm_sec=30, tm_wday=2, tm_yday=234, tm_isdst=-1)
- datetime
datime是time的升级版,可以对date(日期)、time(时间)、datetime(日期时间)等三种单独管理。主要是由下面四个类组成
datetime.datetime常用函数(datetime.date datetime.time通用)
#datetime.datetime.today() 默认返回当前日期和时间的对象,也可以自定义日期和时间 >>> import datetime >>> today = datetime.datetime.today() >>> print today 2018-08-22 18:12:14.568491 >>> last = datetime.datetime(2018,8,22,18,12,14) #注意此处表示日期只能是实际月份,不能带0 >>> print last 2018-08-22 18:12:14 >>> last = datetime.datetime(2018,08,22,18,12,14) File "<input>", line 1 last = datetime.datetime(2018,08,22,18,12,14) ^ SyntaxError: invalid token #datetime.datetime.now() 返回当前时间 >>> datetime.datetime.now() datetime.datetime(2018, 8, 22, 18, 16, 16, 660619) # datetime.strftime(format) #自定义格式化时间 >>> today.strftime("%I:%M:%S %p %d/%m/%Y") '06:12:14 PM 22/08/2018' # datetime.datetime.timple() 将时间转换为struct_time 格式 >>>today.timetuple() time.struct_time(tm_year=2018, tm_mon=8, tm_mday=22, tm_hour=18, tm_min=12, tm_sec=14, tm_wday=2, tm_yday=234, tm_isdst=-1) #datetime.replace()返回一个替换后的date对象 >>>last = today.replace(1949,10,1) >>> print last 1949-10-01 18:12:14.568491 >>> last = today.replace(year=1919,month=3,day=2) >>> print last 1919-03-02 18:12:14.568491 # datetime.datetime.strptime 将字符串转换为日志格式对象 >>> a = a = "2018-08-22 18:25:19" >>> b = datetime.datetime.strptime(a,"%Y-%m-%d %H:%M:%S") >>> print(a) 2018-08-22 18:25:19 >>> print(b) 2018-08-22 18:25:19 >>> print(type(a)) <class 'str'> >>> print(type(b)) <class 'datetime.datetime'>
datetime.timedelta 时间运算
可用参数:days seconds microseconds milliseconds minutes hours weeks
today = datetime.datetime.now() >>> print(today) 2018-08-22 18:26:20.360822 >>> yesterday = today - datetime.timedelta(days=1) >>> print(yesterday) 2018-08-21 18:26:20.360822 >>> last_hour = today - datetime.timedelta(hours=1) >>> print(last_hour) 2018-08-22 17:26:20.360822阅读更多
相关文章推荐
- Python:时间模块:time和datetime
- python 时间模块 time 与 datetime 小结
- python时间处理模块 datetime time模块 deltetime模块
- python学习笔记:time()和datetime()时间模块的基本使用和示例
- Python时间日期格式化之time与datetime模块总结
- Python使用 time和datetime模块随意操作 时间
- Python时间模块datetime、time、calendar的使用方法
- python 时间模块小结 time and datetime
- Python模块知识2:时间日期日历模块Time、Datetime、Calendar
- python 时间模块time,datetime
- Python时间模块datetime、time、calendar的使用方法
- python时间模块-time和datetime
- Python中time模块和datetime模块的常用操作以及几种常用时间格式间的转换
- Python3之时间模块time & datetime & calendar
- Python常用模块-时间模块(time&datetime)
- Python模块知识:时间日期日历模块Time、Datetime、Calendar
- 【Python】随机模块random & 日期时间のtime&&datetime
- Python中time和datetime模块对时间的操作
- python的time和datetime模块 之 时间形式互转
- python时间模块time和datetime