日期和时间处理(Python实现)

网友投稿 626 2022-09-28

日期和时间处理(Python实现)

日期和时间处理(Python实现)

目录

​​1 简单入门​​

​​1.1 获取当前时间​​

​​1.2 获取当前日期​​

​​1.3 datetime中的类​​

​​2 datetime中的常用的类​​

​​2.1  date类​​

​​2.2 time类 ​​

​​2.3 datetime类 ​​

​​2.4 datetime.timedelta类​​

1 简单入门

1.1 获取当前时间

import datetimedatetime_object = datetime.datetime.now()print(datetime_object)

2022-03-29 16:36:44.749582Process finished with exit code 0

1.2 获取当前日期

import datetimedate_object = datetime.date.today()print(date_object)

2022-03-29Process finished with exit code 0

1.3 datetime中的类

import datetimeprint(dir(datetime))

['MAXYEAR', 'MINYEAR', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'date', 'datetime', 'datetime_CAPI', 'sys', 'time', 'timedelta', 'timezone', 'tzinfo']Process finished with exit code 0

2 datetime中的常用的类

date类time类datetime类timedelta类

2.1  date类

(1)示例化date对象

import datetimed = datetime.date(2022, 3, 29)print(d)

2022-03-29Process finished with exit code 0

(2)获取当前日期

from datetime import datetoday = date.today()print("当前日期 =", today)

当前日期 = 2022-03-29Process finished with exit code 0

(3)从时间戳获取日期

我们还可以从时间戳创建日期对象。Unix时间戳是特定日期到UTC的1970年1月1日之间的秒数。可以使用fromtimestamp()方法将时间戳转换为日期。

from datetime import datetimestamp = date.fromtimestamp(1576244364)print("日期 =", timestamp)

日期 = 2019-12-13Process finished with exit code 0

(4)打印今天的年,月和日

from datetime import date# 今天的日期对象today = date.today() print("当前年:", today.year)print("当前月:", today.month)print("当前日:", today.day)

当前年: 2022当前月: 3当前日: 29Process finished with exit code 0

2.2 time类

(1)从time类示例化的时间对象表示本地时间。

from datetime import time# time(hour = 0, minute = 0, second = 0)a = time()print("a =", a)# time(hour, minute and second)b = time(11, 34, 56)print("b =", b)# time(hour, minute and second)c = time(hour = 11, minute = 34, second = 56)print("c =", c)# time(hour, minute, second, microsecond)d = time(11, 34, 56, 234566)print("d =", d)

a = 00:00:00b = 11:34:56c = 11:34:56d = 11:34:56.234566Process finished with exit code 0

(2)打印时,分,秒和微秒

from datetime import timea = time(11, 34, 56)print("小时=", a.hour)print("分钟=", a.minute)print("秒=", a.second)print("微秒=", a.microsecond)

小时= 11分钟= 34秒= 56微秒= 0Process finished with exit code 0

2.3 datetime类

(1)datetime模块有一个名为的dateclass类,可以包含来自date和time对象的信息。

from datetime import datetime#datetime(year, month, day)a = datetime(2019, 11, 28)print(a)# datetime(year, month, day, hour, minute, second, microsecond)b = datetime(2019, 11, 28, 23, 55, 59, 342380)print(b)

2019-11-28 00:00:002019-11-28 23:55:59.342380Process finished with exit code 0

(2)打印年,月,时,分和时间戳

from datetime import datetimea = datetime(2022, 3, 29, 23, 55, 59, 342380)print("年 =", a.year)print("月 =", a.month)print("日 =", a.day)print("时 =", a.hour)print("份 =", a.minute)print("时间戳 =", a.timestamp())

年 = 2022月 = 3日 = 29时 = 23份 = 55时间戳 = 1648569359.34238Process finished with exit code 0

2.4 datetime.timedelta类

(1)timedelta对象表示两个日期或时间之间的时差。

from datetime import datetime, datet1 = date(year = 2018, month = 7, day = 12)t2 = date(year = 2017, month = 12, day = 23)t3 = t1 - t2print("t3 =", t3)t4 = datetime(year = 2018, month = 7, day = 12, hour = 7, minute = 9, second = 33)t5 = datetime(year = 2019, month = 6, day = 10, hour = 5, minute = 55, second = 13)t6 = t4 - t5print("t6 =", t6)print("type of t3 =", type(t3)) print("type of t6 =", type(t6))

t3 = 201 days, 0:00:00t6 = -333 days, 1:14:20type of t3 = type of t6 = Process finished with exit code 0

(2)两个timedelta对象之间的时间差

from datetime import timedeltat1 = timedelta(weeks = 2, days = 5, hours = 1, seconds = 33)t2 = timedelta(days = 4, hours = 11, minutes = 4, seconds = 54)t3 = t1 - t2print("t3 =", t3)

t3 = 14 days, 13:55:39Process finished with exit code 0

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:520拿去表白,表白不成功你打我(Matlab代码动态图)
下一篇:Spring Cloud Alibaba使用Nacos作为注册中心和配置中心
相关文章

 发表评论

暂时没有评论,来抢沙发吧~