![]() | 时间表达与转换 |
![d1f6382c2f9a883f7c302265df6b907a.png](https://img-blog.csdnimg.cn/img_convert/d1f6382c2f9a883f7c302265df6b907a.png)
该篇的内容是关于python中时间的不同表达方式,以及它们间的相互转换和函数封装。
先来举个栗子:
给你一个时间戳282758400.0 ,请告诉我这一天是什么重要日子?
![7d583a6edce8e1114fedc6a2d857e750.png](https://img-blog.csdnimg.cn/img_convert/7d583a6edce8e1114fedc6a2d857e750.png)
你肯定是一脸懵逼!如果换成是这样的表达呢:
1978-12-18 00:00:00'
嗯,1978年的字眼,恍然大悟,今年是改革开放40周年呀!祖国万岁,万万岁!
![df921b5fb115d3e11051d06383647298.png](https://img-blog.csdnimg.cn/img_convert/df921b5fb115d3e11051d06383647298.png)
所以到这,你大致明白本文要做的事情了:介绍时间模块(time模块、datetime模块);表达方式转换。
01Time模块
(一)类型:
time模块,是python封装较早的模块,其中时间表现的格式主要有三种:
timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。
例如:282758400
struct_time时间元组,共有九个元素组。这是计算机可以操作的形式。
例如:(tm_year=1978, tm_mon=12, tm_mday=18, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=352, tm_isdst=-1)
format time 格式化时间,已格式化的结构使时间更具可读性。包括自定义格式和固定格式
例如:'1978-12-18 00:00:00'
(二)转换:
关于3者间的转换,网上有很好的总结了!这里借用一篇博客中的转换图来说明。
![91606b77705c4139b52afaabdb7bb61a.png](https://img-blog.csdnimg.cn/img_convert/91606b77705c4139b52afaabdb7bb61a.png)
这个转换像极了我们数学中用的三角转换,这里简单地将前面的例子做个转换展示:
![e6bfdfa6b90b95248bbfbcb956a2e9cf.png](https://img-blog.csdnimg.cn/img_convert/e6bfdfa6b90b95248bbfbcb956a2e9cf.png)
结果为:
![1010b5af1712b13cc0805f6f40e02c24.png](https://img-blog.csdnimg.cn/img_convert/1010b5af1712b13cc0805f6f40e02c24.png)
稍微改进点的话,可以间转换打包为函数,如定义一个str2stamp函数:输入字符格式的时间,返回时间戳。至于其它的转换也类似,并且简单的。
![7cfa7078b400873ac556ea1c26e9c6b8.png](https://img-blog.csdnimg.cn/img_convert/7cfa7078b400873ac556ea1c26e9c6b8.png)
02
Datetime模块
Datetime模块相比time模块是封装的更晚,各有所长,用起来非常方便。它把python面向对象编程的概念表达地更好。它包括date,time,datetime,timedelta等几个对象操作。
date类
today=datetime.date.today()
print(today)
# print(dir(today))
输出2019-01-05
它有一系列的方法和属性可以直接调用,是非常方便的地方。比如说:
d1 = date(2011,06,03)#date对象
d1.year、date.month、date.day:年、月、日;
d1.replace(year, month, day):生成一个新的日期对象,用参数指定的年,月,日代替原有对象中的属性。(原有对象仍保持不变)
d1.timetuple():返回日期对应的time.struct_time对象;
d1.weekday():返回weekday,如果是星期一,返回0;如果是星期2,返回1,以此类推;
d1.isoweekday():返回weekday,如果是星期一,返回1;如果是星期2,返回2,以此类推;
d1.isocalendar():返回格式如(year,month,day)的元组;
d1.isoformat():返回格式如'YYYY-MM-DD’的字符串;
d1.strftime(fmt):和time模块format相同。
但这里有一个问题,我们不是码农,不可能去记忆这么多的属性和方法呀,解决这个问题的话大家知道使用dir()函数就欧克了print(dir(today))
![4c4c95ab1bc4bf270b768dc6e5d5b686.png](https://img-blog.csdnimg.cn/img_convert/4c4c95ab1bc4bf270b768dc6e5d5b686.png)
里面有给出了各种属性,学过英语的都看得懂,即使不懂可以针对地baidu,问题不大!
2.time类
t1 = datetime.time(10,23,15)#time对象
t1.hour、t1.minute、t1.second时、分、秒
time类和date几乎一样,就是把日期换成时间
3.datetime类
datetime结合了date和time的属性,在平常使用中属于最常见的。以now()函数来举个列子。
time_1978=datetime.datetime.fromtimestamp(282758400)
time_now=datetime.datetime.now()
print(time_now)
print(dir(time_now))
输出为:
![7677fffa4b4bdf2d0c43840fdf6d424b.png](https://img-blog.csdnimg.cn/img_convert/7677fffa4b4bdf2d0c43840fdf6d424b.png)
常见属性为:
datetime.today():返回一个表示当前本地时间的datetime对象;
datetime.now([tz]):返回一个表示当前本地时间的datetime对象,如果提供了参数tz,则获取tz参数所指时区的本地时间;
datetime.utcnow():返回一个当前utc时间的datetime对象;#格林威治时间
datetime.fromtimestamp(timestamp[, tz]):根据时间戮创建一个datetime对象,参数tz指定时区信息;
datetime.utcfromtimestamp(timestamp):根据时间戮创建一个datetime对象;
datetime.combine(date, time):根据date和time,创建一个datetime对象;
datetime.strptime(date_string, format):将格式字符串转换为datetime对象;
4.类型转换
举个典型的例子吧,看着low,但挺实用!
beginDate='2018-10-11 13:13:13'
endDate='2018-10-12 14:13:13
'format = "%Y-%m-%d %H:%M:%S"
starttime = datetime.datetime.strptime(beginDate, format)
endtime = datetime.datetime.strptime(endDate, format)
ret = endtime -starttime
print(ret.days,ret.seconds,ret.total_seconds())
print(dir(ret)
非常方便的是datetime时间格式是可以直接做加减的,并且可以输出相应的属性,也可以dir()查看。
主要参考资料
https://www.cnblogs.com/tkqasn/p/6001134.html
https://www.bilibili.com/video/av33968980?from=search&seid=11081533365567747283
https://www.imooc.com/learn/317
![]() | 本文完 |
![304773802893dfb2acc3136c996d28a3.png](https://img-blog.csdnimg.cn/img_convert/304773802893dfb2acc3136c996d28a3.png)