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

图片

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

web安全工具库

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值