温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python中time和datetime库如何使用

发布时间:2021-07-10 13:52:23 来源:亿速云 阅读:238 作者:Leah 栏目:大数据

今天就跟大家聊聊有关Python中time和datetime库如何使用,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

时间戳

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

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

import time  time.time() #返回当前时间的时间戳 2000年时间戳 都是10位 1557212908.1839943 # 获取毫秒级别的时间戳(爬虫参数基本用14位时间戳加密) int(round(time.time() * 1000)) 1557212908183

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

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

import time  # 字符类型的时间 t = '2019-05-07 15:08:28' # 转为时间数组 timeArray = time.strptime(t, "%Y-%m-%d %H:%M:%S") print(timeArray) # timeArray可以调用tm_year等 print(timeArray.tm_year)   # 2019 # 转为时间戳 timeStamp = int(time.mktime(timeArray)) print(timeStamp)  # 1381419600 # 结果如下 time.struct_time(tm_year=2019, tm_mon=5, tm_mday=7, tm_hour=15, tm_min=8, tm_sec=28, tm_wday=1, tm_yday=127, tm_isdst=-1) 2019 1557212908

「字符串格式更改」

# "2019-05-07 15:08:28" 改为 "2019/5/7 15:08:28" # 先转换为时间数组 import time str_time = "2019-05-07 15: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)   # 2019/05/07 15:08:28 now = datetime.datetime.now() otherStyleTime = now.strftime("%Y--%m--%d %H:%M:%S") print(otherStyleTime) # 2019--05--07 15:08:28

「时间戳转时间日期」

import time  import datetime  # 使用time timeStamp = 1557212908 timeArray = time.localtime(timeStamp) otherStyleTime = time.strftime("%Y--%m--%d %H:%M:%S", timeArray) print(otherStyleTime)   # 2019--05--07 15:08:28 # 使用datetime timeStamp = 1557212908 dateArray = datetime.datetime.fromtimestamp(timeStamp) otherStyleTime = dateArray.strftime("%Y--%m--%d %H:%M:%S") print(otherStyleTime)   # 2019--05--07 15:08:28 # 使用datetime,指定utc时间,相差8小时 timeStamp = 1557212908 dateArray = datetime.datetime.utcfromtimestamp(timeStamp) otherStyleTime = dateArray.strftime("%Y--%m--%d %H:%M:%S") print(otherStyleTime)   # 2019--05--07 07:08:28

Datetime

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

date类

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

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

示例:

from datetime import date 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]]]])

示例:

from datetime import time 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的构造函数中的一样,要注意参数值的范围。

「获取当前日期和时间」

>>> from datetime import datetime >>> now = datetime.now() # 获取当前datetime >>> print(now) 2019-05-07 16:28:07.198690 >>> print(type(now)) <class 'datetime.datetime'>

timedelta类

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

  • (1) days:天数。

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

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

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

import datetime as dt date1=dt.date(2020,10,28) timedel=dt.timedelta(days=4) print('四天后的日期是:', date1+timedel) 四天后的日期是:2020-11-01

看完上述内容,你们对Python中time和datetime库如何使用有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI