Python库中关于时间的常见操作有哪些(python,编程语言)

时间:2024-05-09 09:33:36 作者 : 石家庄SEO 分类 : 编程语言
  • TAG :

time包

importtime

时间戳

从1970年1月1日00:00:00标准时区诞生到现在时间一共过了多少秒。

代码

timestamp=time.time()#type:floatprint(timestamp,type(timestamp))

执行结果

Python库中关于时间的常见操作有哪些

睡眠

有时候可能我们可能需要模仿一些IO请求,假装让程序休眠一下,所以需要用到time的sleep函数。

代码

#睡眠1秒time.sleep(1)

本地时区

本地时区需要用到time的localtime方法。

代码

t=time.localtime()#type:time.struct_timeprint(t,type(t))

执行结果

Python库中关于时间的常见操作有哪些

localtime还能接收一个时间戳参数。

代码

#将时间戳转换成struct_time对象t=time.localtime(1606395685.1878598)#type:time.struct_timeprint(t,type(t))

执行结果

Python库中关于时间的常见操作有哪些

简单的时间格式

代码

t=time.ctime()#type:strprint(t,type(t))

执行结果

Python库中关于时间的常见操作有哪些

虽然这个能把时间展示出来,但是这个格式实在是不是太好看。

同理,time.ctime()也是可以接收一个时间戳的。

代码

t=time.ctime(1606395685.1878598)#type:strprint(t,type(t))

执行结果

Python库中关于时间的常见操作有哪些

时间格式化

日期格式 -> 字符串(strftime)

代码

t=time.localtime()#type:time.struct_timet_str=time.strftime("%Y-%m-%d",t)#type:strprint(t_str,type(t_str))

执行结果

Python库中关于时间的常见操作有哪些

字符串日期 -> 日期(strptime)

代码

t_str="2020-11-02"t_time=time.strptime(t_str,"%Y-%m-%d")#type:time.struct_timeprint(t_time,type(t_time))

执行结果

Python库中关于时间的常见操作有哪些

格式化补充

主要有如下格式

Python库中关于时间的常见操作有哪些

具体详见:

https://www.runoob.com/python/python-date-time.html

datetime包

注:datetime和time是两个不同的类型,不能混用。

fromdatetimeimportdatetime

datetime.today()

代码

t=datetime.today()#type:datetimeprint(t,type(t))print(t.year)#年份print(t.month)#月份

执行结果

Python库中关于时间的常见操作有哪些

datetime.now()

和datetime.today()基本一样,返回的是本地时间。

代码

t=datetime.now()#type:datetimeprint(t,type(t))

执行结果

Python库中关于时间的常见操作有哪些

datetime.utcnow()

utcnow返回的是标准(UTC)时间,上述俩返回的都是本地时间,我们是东八区!

代码

t=datetime.now()print("东八区时间:",t)t=datetime.utcnow()#type:datetimeprint("UTC时间:",t)

执行结果

Python库中关于时间的常见操作有哪些

时间戳转datetime

有时候,我们拿到的,就是时间戳,那就只能转了。

代码

#时间戳timestamp=time.time()print(f"timestamp:{timestamp},type:{type(timestamp)}")#时间戳转datetimet=datetime.fromtimestamp(timestamp)print(f"t:{t},type:{type(t)}")

执行结果

Python库中关于时间的常见操作有哪些

datetime -> 字符串日期(strftime)

代码

fromdatetimeimportdatetimet=datetime.now()str_datetime=t.strftime("%Y-%m-%d%H:%M:%S")print(f"字符串日期:{str_datetime},type:{type(str_datetime)}")

执行结果

Python库中关于时间的常见操作有哪些

字符串日期 -> datetime(strptime)

代码

fromdatetimeimportdatetimestr_datetime="2020-11-2922:05:20"t=datetime.strptime(str_datetime,"%Y-%m-%d%H:%M:%S")print(f"t:{t},type:{type(t)}")

执行结果

Python库中关于时间的常见操作有哪些

时间加减

这才是本次的重头戏,好像只有datetime这个包,才有时间加减的。

时间加减的具体用途很多,必须多久过期什么的,多久之后提醒,都需要提前计算时间,还是很重要的。

代码

fromdatetimeimportdatetimeimportdatetimeasidatetimet=datetime.now()print(f"当前时间:{t}")three_day=t+idatetime.timedelta(days=3)print(f"三天后时间:{three_day}")

执行结果

Python库中关于时间的常见操作有哪些

可以发现,这个时间确实是+成功了。

但是自带的时间加减,有个题,只能加天,不能加月,甚至年。

如果想要时间+月等,还要自己写逻辑。

Python库中关于时间的常见操作有哪些

datetime时间自由加减

有个包正好解决了这个问题。

安装

pipinstallpython-dateutil

代码

fromdatetimeimportdatetimefromdateutil.relativedeltaimportrelativedeltat=datetime.now()print(f"当前时间:{t}")three_time=t+relativedelta(months=3)print(f"三个月后时间:{three_time}")one_year=t+relativedelta(years=1)print(f"一年后时间:{one_year}")up_year=t+relativedelta(years=-1)print(f"去年这个时间:{up_year}")

执行结果

Python库中关于时间的常见操作有哪些

用法很简单,如果想加月/年份,就写正数,如果想减,就写负数,这个方法基本上将python在操作时间上的缺点给弥补了。

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:Python库中关于时间的常见操作有哪些的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:如何使用MASK实现视频弹幕人物遮罩过滤下一篇:

9 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18