• Python学习 -- datetime模块


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

    date 类

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

    date.today()

    获取当前日期。

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

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

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

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

    1. from datetime import date
    2. current_date = date.today()
    3. year = current_date.year
    4. month = current_date.month
    5. day = current_date.day
    6. print(f"Year: {year}, Month: {month}, Day: {day}")

    time 类

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

    time(hour, minute, second, microsecond)

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

    1. from datetime import time
    2. custom_time = time(14, 30, 0)
    3. print(custom_time)
    4. time.hour, time.minute, time.second, time.microsecond

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

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

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

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

    获取当前日期和时间。

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

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

    1. from datetime import datetime
    2. current_datetime = datetime.now()
    3. year = current_datetime.year
    4. month = current_datetime.month
    5. day = current_datetime.day
    6. hour = current_datetime.hour
    7. minute = current_datetime.minute
    8. second = current_datetime.second
    9. microsecond = current_datetime.microsecond
    10. 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 对象。

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

    获取时间差的总秒数。

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

    timezone 类

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

    timezone.utc

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

    1. from datetime import datetime, timezone
    2. utc_time = datetime.now(timezone.utc)
    3. print(utc_time)
    4. timezone(timedelta)

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

    1. from datetime import datetime, timezone, timedelta
    2. custom_timezone = timezone(timedelta(hours=5, minutes=30))
    3. custom_time = datetime.now(custom_timezone)
    4. print(custom_time)

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

    图片

  • 相关阅读:
    微信小程序-form表单-获取用户输入文本框的值
    一些工具/网站自用总结
    Cholesterol-PEG-DBCO,CLS-PEG-DBCO,胆固醇-聚乙二醇-二苯基环辛炔一种环炔烃
    算法(一)
    【油猴脚本 Greasemonkey】GM_xmlhttpRequest内部实现原理
    前端工程化以及WebPack的使用
    GB/T 26518-2023 高分子增强复合防水片材检测
    RDD编程进阶自定义累加器_大数据培训
    Spring项目结合Maven【实现读取不同的资源环境】
    Due to a bug fix in https://github.com/huggingface/transformers/pull/28687
  • 原文地址:https://blog.csdn.net/weixin_41489908/article/details/132715357