Python学习 -- datetime模块

当涉及到处理日期和时间数据时,Python的datetime模块提供了一系列类来帮助您执行各种操作。以下是各个类及其常用方法的详细介绍:

date 类

date 类表示一个年、月、日的日期对象。以下是一些常用的 date 类方法:

date.today()

获取当前日期。

from datetime import date``current_date = date.today()``print(current_date)  # 输出格式:YYYY-MM-DD``date(year, month, day)

创建一个指定年、月、日的日期对象。

from datetime import date``custom_date = date(2023, 9, 4)``print(custom_date)``date.year, date.month, date.day

获取日期对象的年、月、日。

from datetime import date``current_date = date.today()``year = current_date.year``month = current_date.month``day = current_date.day``print(f"Year: {year}, Month: {month}, Day: {day}")

time 类

time 类表示一个时、分、秒的时间对象。以下是一些常用的 time 类方法:

time(hour, minute, second, microsecond)

创建一个指定时、分、秒、微秒的时间对象。

from datetime import time``custom_time = time(14, 30, 0)``print(custom_time)``time.hour, time.minute, time.second, time.microsecond

获取时间对象的时、分、秒、微秒。

from datetime import time``current_time = time(14, 30, 15, 500)``hour = current_time.hour``minute = current_time.minute``second = current_time.second``microsecond = current_time.microsecond``print(f"Hour: {hour}, Minute: {minute}, Second: {second}, Microsecond: {microsecond}")``datetime 类``datetime 类结合了日期和时间信息,表示一个特定的日期和时间。以下是一些常用的 datetime 类方法:``datetime(year, month, day, hour, minute, second, microsecond)

创建一个指定日期和时间的 datetime 对象。

from datetime import datetime``custom_datetime = datetime(2023, 9, 4, 14, 30, 0)``print(custom_datetime)``datetime.now()

获取当前日期和时间。

from datetime import datetime``current_datetime = datetime.now()``print(current_datetime)  # 输出格式:YYYY-MM-DD HH:MM:SS.microsecond``datetime.year, datetime.month, datetime.day, datetime.hour, datetime.minute, datetime.second, datetime.microsecond

获取 datetime 对象的各个部分,包括年、月、日、时、分、秒、微秒。

from datetime import datetime``current_datetime = datetime.now()``year = current_datetime.year``month = current_datetime.month``day = current_datetime.day``hour = current_datetime.hour``minute = current_datetime.minute``second = current_datetime.second``microsecond = current_datetime.microsecond``print(f"Year: {year}, Month: {month}, Day: {day}, Hour: {hour}, Minute: {minute}, Second: {second}, Microsecond: {microsecond}")

timedelta 类

timedelta 类用于表示两个日期或时间之间的时间差。以下是一些常用的 timedelta 类方法:

timedelta(days, seconds, microseconds, milliseconds, minutes, hours, weeks)

创建一个指定时间差的 timedelta 对象。

from datetime import timedelta``time_difference = timedelta(days=7, hours=2)``print(time_difference)``timedelta.total_seconds()

获取时间差的总秒数。

from datetime import timedelta``time_difference = timedelta(days=7, hours=2)``total_seconds = time_difference.total_seconds()``print(f"Total seconds: {total_seconds}")

timezone 类

timezone 类用于表示时区信息。您可以使用它来创建带有时区信息的 datetime 对象。以下是一些常用的 timezone 类方法:

timezone.utc

表示协调世界时(UTC)时区。

from datetime import datetime, timezone``utc_time = datetime.now(timezone.utc)``print(utc_time)``timezone(timedelta)

通过给定的时间差创建一个自定义时区。

from datetime import datetime, timezone, timedelta``custom_timezone = timezone(timedelta(hours=5, minutes=30))``custom_time = datetime.now(custom_timezone)``print(custom_time)

这些 datetime 模块中的类和方法提供了强大的日期和时间处理功能,使您能够轻松执行各种日期和时间操作。希望这些示例可以帮助您更好地理解和使用它们。

欢 迎 关 注

欢迎关注公众号:编程者吧

本文转自 https://mp.weixin.qq.com/s/5JG3aZumzob4eQH119Fi-w,如有侵权,请联系删除。

---------------------------END---------------------------

题外话

在这里插入图片描述

感兴趣的小伙伴,赠送全套Python学习资料,包含面试题、简历资料等具体看下方。

CSDN大礼包:全网最全《Python学习资料》免费赠送!(安全链接,放心点击)

一、Python所有方向的学习路线

Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照下面的知识点去找对应的学习资源,保证自己学得较为全面。

img
img

二、Python必备开发工具

工具都帮大家整理好了,安装就可直接上手!

三、最新Python学习笔记

当我学到一定基础,有自己的理解能力的时候,会去阅读一些前辈整理的书籍或者手写的笔记资料,这些笔记详细记载了他们对一些技术点的理解,这些理解是比较独到,可以学到不一样的思路。

img

四、Python视频合集

观看全面零基础学习视频,看视频学习是最快捷也是最有效果的方式,跟着视频中老师的思路,从基础到深入,还是很容易入门的。

img

五、实战案例

纸上得来终觉浅,要学会跟着视频一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。

img

六、面试宝典

在这里插入图片描述

在这里插入图片描述

简历模板

CSDN大礼包:全网最全《Python学习资料》免费赠送!(安全链接,放心点击)

若有侵权,请联系删除

你可能感兴趣的:(python,学习,java)