失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > python时间库_Python处理日期时间的标准库:time和datetime

python时间库_Python处理日期时间的标准库:time和datetime

时间:2020-12-26 07:38:23

相关推荐

python时间库_Python处理日期时间的标准库:time和datetime

本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理

以下文章来源于Python之王 ,作者小sen

前言

time和datetime是Python处理日期和时间的标准库。在我们项目的实际开发当中使用率是非常高的。datetime模块有四个重要的类,date,time,datetime,以及timedelta类,分别表示的是日期对象、时间对象、日期时间对象和时间差对象。

时间戳

时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数。注意:目前Python中支持的最大的时间戳为32535244799(3001-01-01 15:59:59)

Python的time块下有很多函数可以转换常见日期格式。如函数time.time()用于获取当前时间戳。例如:

importtime

time.time()#返回当前时间的时间戳2000年时间戳都是10位

1557212908.1839943

#获取毫秒级别的时间戳(爬虫参数基本用14位时间戳加密)

int(round(time.time()*1000))

1557212908183

在编写代码时,往往涉及时间、日期、时间戳的相互转换。

「str类型的日期转换为时间戳」

importtime

#字符类型的时间

t='-05-0715:08:28'

#转为时间数组

timeArray=time.strptime(t,"%Y-%m-%d%H:%M:%S")

print(timeArray)

#timeArray可以调用tm_year等

print(timeArray.tm_year)#

#转为时间戳

timeStamp=int(time.mktime(timeArray))

print(timeStamp)#1381419600

#结果如下

time.struct_time(tm_year=,tm_mon=5,tm_mday=7,tm_hour=15,tm_min=8,tm_sec=28,tm_wday=1,tm_yday=127,tm_isdst=-1)

1557212908

「字符串格式更改」

#"-05-0715:08:28"改为"/5/715:08:28"

#先转换为时间数组

importtime

str_time="-05-0715:08:28"

time_array=time.strptime(str_time,"%Y-%m-%d%H:%M:%S")

other_way_time=time.strftime("%Y/%m/%d%H:%M:%S",time_array)

print(other_way_time)#/05/0715:08:28

now=datetime.datetime.now()

otherStyleTime=now.strftime("%Y--%m--%d%H:%M:%S")

print(otherStyleTime)#--05--0715:08:28

「时间戳转时间日期」

importtime

importdatetime

#使用time

timeStamp=1557212908

timeArray=time.localtime(timeStamp)

otherStyleTime=time.strftime("%Y--%m--%d%H:%M:%S",timeArray)

print(otherStyleTime)#--05--0715:08:28

#使用datetime

timeStamp=1557212908

dateArray=datetime.datetime.fromtimestamp(timeStamp)

otherStyleTime=dateArray.strftime("%Y--%m--%d%H:%M:%S")

print(otherStyleTime)#--05--0715:08:28

#使用datetime,指定utc时间,相差8小时

timeStamp=1557212908

dateArray=datetime.datetime.utcfromtimestamp(timeStamp)

otherStyleTime=dateArray.strftime("%Y--%m--%d%H:%M:%S")

print(otherStyleTime)#--05--0707:08:28

Datetime

datetime模块是在time模块的基础之上做了封装,提供了更多更好用的类供我们使用,常用的有date、time、datetime、timedelta、tzinfo。但是为了更灵活的处理时间,最好是将time模块和datetime模块中的精髓学习到。

date类

date类:主要用于处理年、月、日。

date类的构造函数如下:datetime.date(year, month, day)

示例:

fromdatetimeimportdate

d=date(1999,9,29)

print(d)

print('year:',d.year)

print('month:',d.month)

print('day:',d.day)

具体输出:

1999-09-29

year:1999

month:9

day:29

time类

time类:主要用于处理小时、分钟、秒。

time类的构造函数如下:datetime.time(hour[,minute[,second[,microsecond[,tzinfo]]]])

示例:

fromdatetimeimporttime

t=time(22,45,59)

print(t)

print('hour:',t.hour)

print('minute:',t.minute)

print('second:',t.second)

print('microsecond:',t.microsecond)#微秒

print('格式化输出:',t.strftime('%H:%M:%S'))

print('Earliest:',datetime.time.min)

print('Latest:',datetime.time.max)

输出:

22:45:59

hour:22

minute:45

second:59

microsecond:0

格式化输出:22:45:59

Earliest:00:00:00

Latest:23:59:59.999999

datetime子类

datetime子类是date对象和time对象的结合体,是datetime的子类,和它的方法很类似。

它的构造函数如下:

datetime.datetime(year,month,day[,hour[,minute[,second[,microsecond[,tzinfo]]]]]),

各参数的含义与date、time的构造函数中的一样,要注意参数值的范围。

「获取当前日期和时间」

>>>fromdatetimeimportdatetime

>>>now=datetime.now()#获取当前datetime

>>>print(now)

-05-0716:28:07.198690

>>>print(type(now))

timedelta类

timedelta类是用来计算二个datetime对象的差值的。可以进行加减乘除运算的,它的实例化方法和datetime对象很类似 此类中包含如下属性:

(1) days:天数。

(2) microseconds:微秒数(>=0并且<1秒)。

(3) seconds:秒数(>=0并且<1天)。

两个date或datetime对象相减就可以返回一个timedelta对象。

importdatetimeasdt

date1=dt.date(,10,28)

timedel=dt.timedelta(days=4)

print('四天后的日期是:',date1+timedel)

四天后的日期是:-11-01

如果觉得《python时间库_Python处理日期时间的标准库:time和datetime》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。