数据蛙数据分析基础学习

python模块——时间模块

2020-01-27  本文已影响0人  Gaafung峰

前言

关于时间模块的用法有很多,个人认为只需要掌握最常用的基本用法即可,即time/datetime模块,包括如何相互转化,计算差值等。
其余较难部分可等到使用时查阅。
文章参考:时间处理

基本用法

1.获取当前时间

import time

curr_time = time.time()
print(curr_time)
1580115963.2457495

显示的是时间戳,是从1970年1月1日午夜开始到现在所经过的时间,精确到了毫秒 https://tool.lu/timestamp/

2..获取时间元组

import time

localtime = time.localtime(time.time())
print("当前时间是 :", localtime)
当前时间是 : time.struct_time(tm_year=2020, tm_mon=1, tm_mday=27, tm_hour=17, tm_min=7, tm_sec=11, tm_wday=0, tm_yday=27, tm_isdst=0)
import time

localtime = time.localtime(time.time())

print('年', localtime.tm_year)
print('月', localtime.tm_mon)
print('日', localtime.tm_mday)
年 2020
月 1
日 27

3. 格式化时间

import time

localtime = time.localtime(time.time())
localtime = time.strftime("%Y-%m-%d %H:%M:%S", localtime)
print(localtime) 
print(type(localtime))
2020-01-27 17:08:24
<class 'str'>
import time

localtime = time.localtime(time.time())
# 格式化成Wed Nov 13 17:45:58 2019形式
print(time.strftime("%a %b %d %H:%M:%S %Y", localtime))
Thu Jan 16 15:34:38 2020
import time

a = '2019-11-13 17:43:11'
a = time.mktime(time.strptime(a,"%Y-%m-%d %H:%M:%S"))
print(a)

4.日期格式化符号

你应该已经注意到了,不论是从时间戳到字符串,还是字符串到时间戳,在日期转换过程中,都用到了类似"%Y-%m-%d %H:%M:%S" 这种的字符串,他们规定了以什么样的方式进行转换

datetime

1.date类

显示到天

from datetime import date

today = date.today()
print(today.year)
print(today.month)
print(today.day)
print(today)
2020
1
16
2020-01-16

2. time类

从小时显示到毫秒

from datetime import time

a = time(19, 36, 30, 345)
print(a.hour)
print(a.minute)
print(a.second)
print(a.microsecond)
19
36
30
345

3. datetime

全显示,建议掌握透彻

from datetime import datetime

now = datetime.now()
print(now)
print(now.year)
print(now.month)
print(now.day)
print(now.hour)
print(now.minute)
print(now.second)
print(now.microsecond)
2020-01-16 15:35:58.023465
2020
1
16
15
35
58
23465

4.转化时间的方法

4.1strftime

datetime→字符串

from datetime import datetime

now = datetime.now()
now_str = now.strftime("%Y-%m-%d %H:%M:%S")
print(now_str)
print(type(now_str))
2020-01-27 17:14:00
<class 'str'>

时间元组→字符串

now = time.localtime()
now_str = time.strftime('%Y-%m-%d %H:%M:%S', now)       # 将时间元组转成字符串
print(now_str)
2020-01-27 17:20:42

4.2 strptime

字符串→datetime

from datetime import datetime

olympics_time = datetime.strptime("2008-08-08 20:08:08", '%Y-%m-%d %H:%M:%S')
print(type(olympics_time), olympics_time)
<class 'datetime.datetime'> 2008-08-08 20:08:08

字符串→时间元组

import time

time_str = '2019-12-31 11:22:23'
res = time.strptime(time_str, '%Y-%m-%d %H:%M:%S')      # 将字符串转成时间元组
print(res, type(res))
time.struct_time(tm_year=2019, tm_mon=12, tm_mday=31, tm_hour=11, tm_min=22, tm_sec=23, tm_wday=1, tm_yday=365, tm_isdst=-1) <class 'time.struct_time'>

4.3 fromtimestamp

时间戳→datetime

from datetime import datetime

a = datetime.fromtimestamp(1575896495)
print(a, type(a))
2019-12-09 21:01:35 <class 'datetime.datetime'>

4.4 timetuple

from datetime import datetime

now = datetime.now()
print(now.ctime())      # 返回ctime() 格式时间字符串
print(now.timetuple())  # 返回时间元组
Thu Jan 16 16:09:48 2020
time.struct_time(tm_year=2020, tm_mon=1, tm_mday=16, tm_hour=16, tm_min=9, tm_sec=48, tm_wday=3, tm_yday=16, tm_isdst=-1)

5.timedelta

建议掌握
计算datetime差值

from datetime import datetime

d1 = datetime(year=2019, month=12, day=9, hour=19, minute=20, second=12)
d2 = datetime(year=2019, month=12, day=7, hour=7, minute=19, second=40)

diff = d1 - d2
print(type(diff), diff)
print(diff.days, diff.seconds) #控制显示层级
<class 'datetime.timedelta'> 2 days, 12:00:32
2 43232

总结

文章内容包括许多,需要注意时间类型及如何转化。

类型的总结

时间函数的几个基本类型有:时间戳 / 时间元组 / 格式化输出的字符串时间 / datetime类型。

datetime类型又分 datetime.datetime / datetime.time / datetime.timedelta(差值)

转化的总结

常用是 strptime / strftime

感谢:有所收获请点赞~

上一篇下一篇

猜你喜欢

热点阅读