展开菜单
首页 精品内容 本月促销 装机必备 Windows PDF教程 macOS软件 IOS软件 Android AI 专题
全部分类

当前位置:

首页 > 编程开发 > Python中datetime模块的使用详解

Python中datetime模块的使用详解

1、日期时间对象日期时间对象是指具有日期(年月日)和时间(时分秒)双重属性的实例日期时间对象的类型为datetime.datetime日期时间对象常用的属性有年、月、日、时、分、秒、微秒日期时间对象可以指定时间创建,也可以通过获取当前时间来创建日期时间对象指定时间创建时可按位置传参创建,也可关键字传参创建日期时间对象的创建函数有datetime.datetime(),datetime.datetime.now()、datetime.datetime.today()、datetime.datetime.ut

本文目录

      1、日期时间对象

      • 日期时间对象是指具有日期(年月日)和时间(时分秒)双重属性的实例

      • 日期时间对象的类型为datetime.datetime

      • 日期时间对象常用的属性有年、月、日、时、分、秒、微秒

      • 日期时间对象可以指定时间创建,也可以通过获取当前时间来创建

      • 日期时间对象指定时间创建时可按位置传参创建,也可关键字传参创建

      • 日期时间对象的创建函数有datetime.datetime(),datetime.datetime.now()、datetime.datetime.today()、datetime.datetime.utcnow()

      • 日期时间对象通过datetime.datetime()创建时的参数依次为:year,month,day,hour,minute,second,microsecond

      • 日期时间对象通过datetime.datetime.now()函数创建不需要参数

      • 日期时间对象通过datetime.datetime.today()函数创建不需要参数

      • 日期时间对象通过datetime.datetime.utcnow()函数创建不需要参数

      • 日期时间对象通过datetime.datetime()创建时至少应该包含年、月、日三个参数

      • 日期时间对象通过datetime.datetime()创建时的参数范围如下

      序号形参实参范围
      1year1~9999
      2month1~12
      3day0~23
      4hour0~23
      5minute0~59
      6second0~59
      7microsecond1~999999

      2、创建日期时间对象

      2.1、通过datetime.datetime.utcnow()创建

      datetime_zero = datetime.datetime.utcnow()

      2.2、通过datetime.datetime.today()函数创建

      datetime_first = datetime.datetime.today()

      2.3、通过datetime.datetime.now()创建

      datetime_second = datetime.datetime.now()

      2.4、通过datetime.datetime()创建

      • 指定日期时间创建

      • 必传年、月、日参数

      • 指定日期时间、位置参数的顺序不可变且参数值必须在规定的范围内

      datetime_three = datetime.datetime(year=1, month=1, day=1, hour=0, minute=0, second=0, microsecond=1)
      datetime_four = datetime.datetime(year=9999, month=12, day=31, hour=23, minute=59, second=59, microsecond=999999)
      datetime_five = datetime.datetime(9999, 12, 31, 23, 59, 59, 999999)

      2.5、查看创建的对象

      print(datetime_zero, type(datetime_zero))       # 2022-07-09 18:12:43.486469 
      print(datetime_first, type(datetime_first))     # 2022-07-09 18:12:43.486469 
      print(datetime_second, type(datetime_second))   # 2022-07-09 18:12:43.486469 
      print(datetime_three, type(datetime_three))     # 0001-01-01 00:00:00.000001 
      print(datetime_four, type(datetime_four))       # 9999-12-31 23:59:59.999999 
      print(datetime_five, type(datetime_five))       # 9999-12-31 23:59:59.999999 

      Python中datetime模块的使用详解

      2.6、查看datetime可以处理的最大的日期时间对象及最小的日期时间对象

      print(datetime.datetime.min)        # 0001-01-01 00:00:00
      print(datetime.datetime.max)        # 9999-12-31 23:59:59.999999

      Python中datetime模块的使用详解

      3、日期事件对象的属性

      datetime_first = datetime.datetime.today()
      """# 从日期时间对象中获取日期属性【年-月-日】"""
      new_time = datetime.datetime.date(datetime_first)
      print(new_time)
      print(type(new_time))
      """# 从日期时间对象中获取时间属性【时:分:秒:微秒】"""
      new_time = datetime.datetime.time(datetime_first)
      print(new_time)
      print(type(new_time))
      """# 从日期时间对象中获取年份"""
      datetime_year = datetime_first.year
      print(datetime_year, type(datetime_year))       # 2022 
      """# 从日期时间对象中获取月份"""
      datetime_month = datetime_first.month
      print(datetime_month, type(datetime_month))       # 7 
      """# 从日期时间对象中获取天"""
      datetime_day = datetime_first.day
      print(datetime_day, type(datetime_day))       # 10 
      """# 从日期时间对象中获取小时"""
      datetime_hour = datetime_first.hour
      print(datetime_hour, type(datetime_hour))       # 18 
      """# 从日期时间对象中获取分钟"""
      datetime_minute = datetime_first.minute
      print(datetime_minute, type(datetime_minute))       # 56 
      """# 从日期时间对象中获取秒数"""
      datetime_second = datetime_first.second
      print(datetime_second, type(datetime_second))       # 16 
      """# 从日期时间对象中获取微秒"""
      datetime_microsecond = datetime_first.microsecond
      print(datetime_microsecond, type(datetime_microsecond))       # 735264 

      “”“# datetime.datetime.date()函数的参数只能是datetime.datetime类型”“”
      date_time = datetime.date(2022, 12, 26)

      “”“# 传入的参数不能为datetime.date类型”“”
      “”“# TypeError: descriptor ‘date’ for ‘datetime.datetime’ objects doesn’t apply to a ‘datetime.date’ object”“”
      “”“# print(datetime.datetime.date(date_time))”“”

      time_time = datetime.time(12, 2, 54, 999999)
      “”“# 传入的参数不能为datetime.time类型”“”
      “”“# TypeError: descriptor ‘date’ for ‘datetime.datetime’ objects doesn’t apply to a ‘datetime.time’ object”“”
      “”“# print(datetime.datetime.date(time_time))”“”
      “”“# 同理,datetime.datetime.time()函数传入的参数亦不能为datetime.date类型和datetime.time类型”“”
      “”“# TypeError: descriptor ‘time’ for ‘datetime.datetime’ objects doesn’t apply to a ‘datetime.date’ object”“”
      “”“# print(datetime.datetime.time(date_time))”“”
      “”“# TypeError: descriptor ‘time’ for ‘datetime.datetime’ objects doesn’t apply to a ‘datetime.time’ object”“”
      “”“# print(datetime.datetime.time(time_time))”""

      Python中datetime模块的使用详解

      4、日期时间对象转换为时间元组

      • 时间元组是指具有 年份、月份、日、小时、分钟、秒数、星期中的第N天、年中的第N天、夏令时标志的一个元组对象

      • 时间元组示例:(tm_year=2022, tm_mon=7, tm_mday=9, tm_hour=19, tm_min=14, tm_sec=27, tm_wday=5, tm_yday=190, tm_isdst=0)

      • 其中tm_wday的值从0开始,范围是:0~6,0为星期一,6为星期日;tm_isdst=0代表未启用夏令时

      UTCDateTime = datetime.datetime(year=2022, month=7, day=10, hour=19, minute=14, second=27, microsecond=1235)
      datetime_UTCTimeTuple = datetime.datetime.utctimetuple(UTCDateTime)
      print(datetime_UTCTimeTuple, type(datetime_UTCTimeTuple))  # 类型为:

      Python中datetime模块的使用详解

      5、将日期时间对象转化为公元历开始计数的天数

      • 日期时间对象转化为公元历开始计数的天数

      • 将一个整形数值转换为日期时间对象

      • 整形数值最大值为3652059

      datetime_replace = datetime.datetime(year=2022, month=7, day=9, hour=19, minute=14, second=27, microsecond=123)
      datetime_ordinal = datetime.datetime.toordinal(datetime_replace)
      print(datetime_ordinal, type(datetime_ordinal))     # 738345 
      print(datetime.datetime.fromordinal(1))     # 0001-01-02 00:00:00
      print(datetime.datetime.fromordinal(2))     # 0001-01-02 00:00:00
      datetime_replace_max = datetime.datetime(year=9999, month=12, day=31, hour=23, minute=59, second=59, microsecond=999999)
      print(datetime.datetime.toordinal(datetime_replace_max))
      print(datetime.datetime.fromordinal(3652060))

      Python中datetime模块的使用详解

      Python中datetime模块的使用详解

      6、日期时间对象转换为一个日期格式值的字符串

      • 示例如 Sat Jul 9 19:14:27 2022(2022年7月9日星期六)

      • 第一部分的值代表星期几

      • 第二部分的值代表月份

      • 第三部分的值代表日

      • 第四部分的值代表时间

      • 第五部分的值代表年份

      datetime_replace = datetime.datetime(year=2022, month=7, day=9, hour=19, minute=14, second=27, microsecond=123)
      print(datetime_replace)
      ctime_datetime = datetime.datetime.ctime(datetime_replace)
      print(ctime_datetime, type(ctime_datetime))
      ```
      ![Python标准库datetime之datetime模块详解_date_07](https://img-blog.csdnimg.cn/b7e257debb0249ca84463b9d73d7dbf1.png)
      ## 7、日期时间对象转换为时间戳
      ```python
      datetime_timestamp = datetime.datetime.timestamp(datetime_replace)
      print(datetime_timestamp, type(datetime_timestamp))         # 1657365267.000123 
      ```
      ![Python标准库datetime之datetime模块详解_datetime_08](https://img-blog.csdnimg.cn/e38e2a5be32242c5a79441e7300e2fc2.png)
      ## 8、时间戳转换为日期时间对象
      ```python
      print(datetime.datetime.fromtimestamp(datetime_timestamp))  # 2022-07-09 19:14:27.000123
      ```
      ![Python标准库datetime之datetime模块详解_datetime_08](https://img-blog.csdnimg.cn/e38e2a5be32242c5a79441e7300e2fc2.png)
      ## 9、日期时间对象转换为时间元组
      ```python
      datetime_timetuple = datetime.datetime.timetuple(datetime_replace)
      print(datetime_timetuple, type(datetime_timetuple))
      ```
      ![Python标准库datetime之datetime模块详解_datetime_08](https://img-blog.csdnimg.cn/e38e2a5be32242c5a79441e7300e2fc2.png)
      ## 10、ISO标准日期时间格式
      ISO标准的日历时间,Calendar中文释义为日历
      * 各个值的含义为(年份、周数、周内的第N天)即(year, week, weekday);
      * weekday的值为[1,7],1代表周一,7代表周日
      * 示例:datetime.IsoCalendarDate(year=2022, week=27, weekday=7)
      ```python
      datetime_replace = datetime.datetime(year=2022, month=7, day=9, hour=19, minute=14, second=27, microsecond=123)
      UTCDateTime = datetime.datetime(year=2022, month=7, day=10, hour=19, minute=14, second=27, microsecond=1235)
      # ISO标准日期时间格式
      print(datetime.datetime.utcoffset(UTCDateTime))
      # 将日期时间对象转换为ISO标准日期时间格式的字符串
      UTC_ISO_DateTime = datetime.datetime.isoformat(UTCDateTime)
      print(UTC_ISO_DateTime, type(UTC_ISO_DateTime))         # 2022-07-10T19:14:27.001235 
      # 将ISO标准日期时间格式的字符串转换为日期时间类型
      From_UTC_ISO_DateTime = datetime.datetime.fromisoformat('9999-12-31T23:59:59.999999')   # 
      print(From_UTC_ISO_DateTime, type(From_UTC_ISO_DateTime))
      # ISO标准的周内第N天
      # 值的范围是[1,7],1代表周一,7代表周日
      UTC_ISO_WeekDateTime = datetime.datetime.isoweekday(UTCDateTime)
      print(UTC_ISO_WeekDateTime, type(UTC_ISO_WeekDateTime))     # 7 
      # ISO标准的日历时间,Calendar中文释义为日历
      # 各个值的含义为(年份、周数、周内的第N天)即(year, week, weekday);
      # weekday的值为[1,7],1代表周一,7代表周日
      # 示例:datetime.IsoCalendarDate(year=2022, week=27, weekday=7)
      UTC_ISO_CalendarDateTime = datetime.datetime.isocalendar(UTCDateTime)
      print(UTC_ISO_CalendarDateTime, type(UTC_ISO_CalendarDateTime))
      # 将ISO标准日历格式的字符串转换为时间日期型
      From_UTC_ISO_CalendarDateTime = datetime.datetime.fromisocalendar(year=2022, week=27, day=7)
      print(From_UTC_ISO_CalendarDateTime)        # 2022-07-10 00:00:00
      print(type(From_UTC_ISO_CalendarDateTime))  # 
      ```
      ![Python标准库datetime之datetime模块详解_python_11](https://img-blog.csdnimg.cn/bb944815182d477a9a662862f13a9f3a.png)
      ## 11、日期时间替换函数replace()
      *  replace()可以只替换日期时间属性的某一项
      * replace()函数的第一个参数必传
      * replace()函数的第一个参数是一个日期时间类型(datetime.datetime)的对象
      * 按关键字传参替换
      * 按位置传参体换
      ```python
      datetime_replace = datetime.datetime(year=2022, month=7, day=9, hour=19, minute=14, second=27, microsecond=123)
      # 初始值
      print(f"datetime_replace的原值为:{datetime_replace}", f"类型是:{type(datetime_replace)}")
      # 不传参数
      print(datetime.datetime.replace(datetime_replace))    # 2022-07-09 19:14:27.000123
      # 只替换年份
      print(datetime.datetime.replace(datetime_replace, 2019))    # 2019-07-09 19:14:27.000123
      print(datetime.datetime.replace(datetime_replace, year=2019))   # 2019-07-09 19:14:27.000123
      # 只替换月份, 替换其他参数同理
      print(datetime.datetime.replace(datetime_replace, month=12))            # 2022-12-09 19:14:27.000123
      print(datetime.datetime.replace(datetime_replace, datetime_replace.year, 12))   # 2022-12-09 19:14:27.000123
      # 替换其他参数同理
      print(datetime.datetime.replace(datetime_replace, year=2019, month=12, day=31, hour=15,
                                      minute=13, second=15, microsecond=9999))    # 2019-12-31 15:13:15.009999
      ```
      ![Python标准库datetime之datetime模块详解_date_12](https://img-blog.csdnimg.cn/4ed28241d33b4928b3a8b2132b08a7d6.png)
      ## 12、日期时间对象格式化strftime()
      * 日期时间对象格式化常用的格式如下
      * %H(两位数的小时)
      * %M(两位数的分钟)
      * %S(两位数的秒)
      * %f(6位数的微秒)
      * %h(简写的月份名,一般为英文简写)
      * %y(两位数的年份)
      * %Y(四位数的年份)
      * %m(两位数的月份)
      * %d(两位数的天数)
      * 可以只格式化部分属性
      ```python
      datetime_replace = datetime.datetime(year=2022, month=7, day=9, hour=19, minute=14, second=27, microsecond=123)
      # 可以只格式化部分属性
      datetime_str = datetime.datetime.strftime(datetime_replace, "%Y-%m-%d %H:%M:%S.%f")
      print(f"格式化后是:{datetime_str}", type(datetime_str))      # 2022-07-09 19:14:27.000123 
      # 格式化日期属性
      datetime_str_date = datetime.datetime.strftime(datetime_replace, "%Y-%m-%d")
      print(f"格式化日期的值为:{datetime_str_date}")      # 2022-07-09
      # 格式时间属性
      datetime_str_time = datetime.datetime.strftime(datetime_replace, "%H:%M:%S.%f")
      print(f"格式化时间的值为:{datetime_str_time}")      # 19:14:27.000123
      ```
      ![Python标准库datetime之datetime模块详解_datetime_13](https://img-blog.csdnimg.cn/4d9da4de3f464f1ca73e30f918406a0a.png)
      ## 附录、完整代码
      ```python
      # coding:utf-8
      import datetime
      # 日期时间对象
      # 日期时间对象是指具有日期(年月日)和时间(时分秒)双重属性的实例
      # 日期时间对象的类型为datetime.datetime
      # 日期时间对象常用的属性有年、月、日、时、分、秒、微秒等
      # 日期时间对象可以指定时间创建,也可以通过获取当前时间来创建
      # 日期时间对象指定时间创建时可按位置传参创建,也可关键字传参创建
      # 日期时间对象的创建函数有datetime.datetime(),datetime.datetime.now()、datetime.datetime.today()、datetime.datetime.utcnow()
      # 日期时间对象通过datetime.datetime()创建时的参数依次为:year,month,day,hour,minute,second,microsecond
      # 日期时间对象通过datetime.datetime.now()函数创建不需要参数
      # 日期时间对象通过datetime.datetime.today()函数创建不需要参数
      # 日期时间对象通过datetime.datetime.utcnow()函数创建不需要参数
      # 日期时间对象通过datetime.datetime()创建时至少应该包含年月日三个参数
      # 日期时间对象通过datetime.datetime()创建时的参数范围如下
      # {year[1~9999]、month[1~12]、day[1~31]、hour[0~23]、minute[0~59]、second[0~59]、microsecond[1~999999]}
      
      # 通过datetime.datetime.utcnow()创建
      datetime_zero = datetime.datetime.utcnow()
      # 通过datetime.datetime.today()函数创建
      datetime_first = datetime.datetime.today()
      # 通过datetime.datetime.now()创建
      datetime_second = datetime.datetime.now()
      # 通过datetime.datetime()函数指定日期时间、关键字传参创建
      datetime_three = datetime.datetime(year=1, month=1, day=1, hour=0, minute=0, second=0, microsecond=1)
      datetime_four = datetime.datetime(year=9999, month=12, day=31, hour=23, minute=59, second=59, microsecond=999999)
      # 通过datetime.datetime()函数指定日期时间、按位置传参创建,顺序不可变且参数值必须在规定的范围内
      datetime_five = datetime.datetime(9999, 12, 31, 23, 59, 59, 999999)
      print(datetime_zero, type(datetime_zero))       # 2022-07-09 18:12:43.486469 
      print(datetime_first, type(datetime_first))     # 2022-07-09 18:12:43.486469 
      print(datetime_second, type(datetime_second))   # 2022-07-09 18:12:43.486469 
      print(datetime_three, type(datetime_three))     # 0001-01-01 00:00:00.000001 
      print(datetime_four, type(datetime_four))       # 9999-12-31 23:59:59.999999 
      print(datetime_five, type(datetime_five))       # 9999-12-31 23:59:59.999999 
      # 查看datetime可以处理的最大的日期时间对象及最小的日期时间对象
      print(datetime.datetime.min)        # 0001-01-01 00:00:00
      print(datetime.datetime.max)        # 9999-12-31 23:59:59.999999
      
      """# 从日期时间对象中获取日期属性【年-月-日】"""
      new_time = datetime.datetime.date(datetime_first)
      print(new_time)
      print(type(new_time))
      """# 从日期时间对象中获取时间属性【时:分:秒:微秒】"""
      new_time = datetime.datetime.time(datetime_first)
      print(new_time)
      print(type(new_time))
      """# 从日期时间对象中获取年份"""
      datetime_year = datetime_four.year
      print(datetime_year, type(datetime_year))       # 9999 
      """# 从日期时间对象中获取月份"""
      datetime_month = datetime_four.month
      print(datetime_month, type(datetime_month))       # 12 
      """# 从日期时间对象中获取天"""
      datetime_day = datetime_four.day
      print(datetime_day, type(datetime_day))       # 31 
      """# 从日期时间对象中获取小时"""
      datetime_hour = datetime_four.hour
      print(datetime_hour, type(datetime_hour))       # 23 
      """# 从日期时间对象中获取分钟"""
      datetime_minute = datetime_four.minute
      print(datetime_minute, type(datetime_minute))       # 59 
      """# 从日期时间对象中获取秒数"""
      datetime_second = datetime_four.second
      print(datetime_second, type(datetime_second))       # 59 
      """# 从日期时间对象中获取微秒"""
      datetime_microsecond = datetime_four.microsecond
      print(datetime_microsecond, type(datetime_microsecond))       # 999999 
      """# datetime.datetime.date()函数的参数只能是datetime.datetime类型"""
      date_time = datetime.date(2022, 12, 26)
      """# 传入的参数不能为datetime.date类型"""
      """# TypeError: descriptor 'date' for 'datetime.datetime' objects doesn't apply to a 'datetime.date' object"""
      """# print(datetime.datetime.date(date_time))"""
      time_time = datetime.time(12, 2, 54, 999999)
      """# 传入的参数不能为datetime.time类型"""
      """# TypeError: descriptor 'date' for 'datetime.datetime' objects doesn't apply to a 'datetime.time' object"""
      """# print(datetime.datetime.date(time_time))"""
      """# 同理,datetime.datetime.time()函数传入的参数亦不能为datetime.date类型和datetime.time类型"""
      """# TypeError: descriptor 'time' for 'datetime.datetime' objects doesn't apply to a 'datetime.date' object"""
      """# print(datetime.datetime.time(date_time))"""
      """# TypeError: descriptor 'time' for 'datetime.datetime' objects doesn't apply to a 'datetime.time' object"""
      """# print(datetime.datetime.time(time_time))"""
      # 将日期时间对象转换为时间元组类型
      # 时间元组是指具有 年份、月份、日、小时、分钟、秒数、星期中的第N天、年中的第N天、夏令时标志的一个元组对象
      # 时间元组示例:(tm_year=2022, tm_mon=7, tm_mday=9, tm_hour=19, tm_min=14, tm_sec=27, tm_wday=5, tm_yday=190, tm_isdst=0)
      # 其中tm_wday的值从0开始,范围是:0~6,0为星期一,6为星期日;tm_isdst=0代表未启用夏令时
      UTCDateTime = datetime.datetime(year=2022, month=7, day=10, hour=19, minute=14, second=27, microsecond=1235)
      datetime_UTCTimeTuple = datetime.datetime.utctimetuple(UTCDateTime)
      print(datetime_UTCTimeTuple, type(datetime_UTCTimeTuple))  # 类型为:
      # 将日期时间对象转化为公元历开始计数的天数
      datetime_replace = datetime.datetime(year=2022, month=7, day=9, hour=19, minute=14, second=27, microsecond=123)
      datetime_ordinal = datetime.datetime.toordinal(datetime_replace)
      print(datetime_ordinal, type(datetime_ordinal))     # 738345 
      print(datetime.datetime.fromordinal(1))     # 0001-01-02 00:00:00
      print(datetime.datetime.fromordinal(2))     # 0001-01-02 00:00:00
      # ctime()是将日期时间类型转换为一个日期之间值的字符串,示例如 Sat Jul  9 19:14:27 2022(2022年7月9日星期六)
      # ctime()返回值的第一部分的值代表星期几,第二部分的值代表月份,第三部分的值代表日,第四部分的值代表时间,第五部分的值代表年份
      print(datetime_replace)
      ctime_datetime = datetime.datetime.ctime(datetime_replace)
      print(ctime_datetime, type(ctime_datetime))
      
      # 将日期时间对象转换为时间戳
      datetime_timestamp = datetime.datetime.timestamp(datetime_replace)
      print(datetime_timestamp, type(datetime_timestamp))         # 1657365267.000123 
      # 将时间戳转换为日期时间对象
      print(datetime.datetime.fromtimestamp(datetime_timestamp))  # 2022-07-09 19:14:27.000123
      
      # 将日期时间对象转换为时间元组
      datetime_timetuple = datetime.datetime.timetuple(datetime_replace)
      print(datetime_timetuple, type(datetime_timetuple))
      # ISO标准日期时间格式
      print(datetime.datetime.utcoffset(UTCDateTime))
      # 将日期时间对象转换为ISO标准日期时间格式的字符串
      UTC_ISO_DateTime = datetime.datetime.isoformat(UTCDateTime)
      print(UTC_ISO_DateTime, type(UTC_ISO_DateTime))         # 2022-07-10T19:14:27.001235 
      # 将ISO标准日期时间格式的字符串转换为日期时间类型
      From_UTC_ISO_DateTime = datetime.datetime.fromisoformat('9999-12-31T23:59:59.999999')   # 
      print(From_UTC_ISO_DateTime, type(From_UTC_ISO_DateTime))
      # ISO标准的周内第N天
      # 值的范围是[1,7],1代表周一,7代表周日
      UTC_ISO_WeekDateTime = datetime.datetime.isoweekday(UTCDateTime)
      print(UTC_ISO_WeekDateTime, type(UTC_ISO_WeekDateTime))     # 7 
      # ISO标准的日历时间,Calendar中文释义为日历
      # 各个值的含义为(年份、周数、周内的第N天)即(year, week, weekday);
      # weekday的值为[1,7],1代表周一,7代表周日
      # 示例:datetime.IsoCalendarDate(year=2022, week=27, weekday=7)
      UTC_ISO_CalendarDateTime = datetime.datetime.isocalendar(UTCDateTime)
      print(UTC_ISO_CalendarDateTime, type(UTC_ISO_CalendarDateTime))
      # 将ISO标准日历格式的字符串转换为时间日期型
      From_UTC_ISO_CalendarDateTime = datetime.datetime.fromisocalendar(year=2022, week=27, day=7)
      print(From_UTC_ISO_CalendarDateTime)        # 2022-07-10 00:00:00
      print(type(From_UTC_ISO_CalendarDateTime))  # 
      
      # 日期时间替换函数replace()
      # replace()可以只替换日期时间属性的某一项
      # replace()函数的第一个参数必传
      # replace()函数的第一个参数是一个日期时间类型(datetime.datetime)的对象
      # 按关键字传参替换
      # 按位置传参体换
      datetime_replace = datetime.datetime(year=2022, month=7, day=9, hour=19, minute=14, second=27, microsecond=123)
      # 初始值
      print(f"datetime_replace的原值为:{datetime_replace}", f"类型是:{type(datetime_replace)}")
      # 不传参数
      print(datetime.datetime.replace(datetime_replace))    # 2022-07-09 19:14:27.000123
      # 只替换年份
      print(datetime.datetime.replace(datetime_replace, 2019))    # 2019-07-09 19:14:27.000123
      print(datetime.datetime.replace(datetime_replace, year=2019))   # 2019-07-09 19:14:27.000123
      # 只替换月份, 替换其他参数同理
      print(datetime.datetime.replace(datetime_replace, month=12))            # 2022-12-09 19:14:27.000123
      print(datetime.datetime.replace(datetime_replace, datetime_replace.year, 12))   # 2022-12-09 19:14:27.000123
      # 替换其他参数同理
      print(datetime.datetime.replace(datetime_replace, year=2019, month=12, day=31, hour=15,
                                     minute=13, second=15, microsecond=9999))    # 2019-12-31 15:13:15.00999
      # 日期时间对象格式化strftime()
      # 日期时间对象格式化常用的格式如下:
      ""
      %H(两位数的小时)、%M(两位数的分钟)、%S(两位数的秒)、%f(6位数的微秒)、%h(简写的月份名,一般为英文简写)
      %y(两位数的年份)、%Y(四位数的年份)、%m(两位数的月份)、%d(两位数的天数)
      """
      # 可以只格式化部分属性
      datetime_str = datetime.datetime.strftime(datetime_replace, "%Y-%m-%d %H:%M:%S.%f")
      print(f"格式化后是:{datetime_str}", type(datetime_str))      # 2022-07-09 19:14:27.000123 
      # 格式化日期属性
      datetime_str_date = datetime.datetime.strftime(datetime_replace, "%Y-%m-%d")
      print(f"格式化日期的值为:{datetime_str_date}")      # 2022-07-09
      # 格式时间属性
      datetime_str_time = datetime.datetime.strftime(datetime_replace, "%H:%M:%S.%f")
      print(f"格式化时间的值为:{datetime_str_time}")      # 19:14:27.000123
      ```
      本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系bd@zhengruan.com
      作者最新文章
      编程开发 Python
      相关文章 更多
      精品专题 更多
      装机必备

      正软商城装机必备专区,精选办公、浏览器、安全防护、影音播放、压缩解压、设计创作和系统工具等电脑常用正版软件,帮助用户快速完成新电脑软件配置。

      Windows

      正软商城Windows软件专区,汇集适用于Windows电脑的办公、设计、安全防护、影音播放、开发工具和系统优化软件,提供软件介绍、系统要求、正版授权及购买下载服务。

      PDF教程

      正软商城PDF教程频道提供PDF编辑、转换、合并、拆分、压缩及格式处理方法,同时介绍常用PDF软件和工具的使用技巧。

      macOS软件

      正软商城macOS软件专区,精选适用于Mac电脑的办公、设计、影音、效率、开发和系统工具,提供软件功能介绍、macOS兼容版本、正版授权及购买下载服务。

      IOS软件

      正软商城iOS软件专区,精选适用于iPhone和iPad的办公、学习、影音、设计、效率及AI应用,提供功能介绍、适用设备、系统要求和正版获取方式等信息。

      AI

      正软商城AI软件专区,汇集AI写作、AI绘画、AI视频、AI办公、AI编程、AI翻译、智能客服和数据分析等人工智能工具,提供功能介绍、适用平台、收费方式及正版购买信息。

      Mac软件 更多
      Aperty macOS版
      Aperty macOS版

      Aperty 是 Skylum 推出的 AI 人像修图软件,支持自然肤质修饰、瑕疵处理、面部与身体调整、预设及批量同步。

      火山引擎 网页版
      火山引擎 网页版

      火山引擎是字节跳动旗下企业级云与AI服务平台。聚焦大模型核心能力,依托字节自研豆包多模态大模型,凭借火山方舟一站式平台,提供模型精调、推理部署、智能应用搭建等全链路服务,结合高性能AI算力,助力各行业快速落地大模型数字化与智能化应用。

      立刻MV 网页版
      立刻MV 网页版

      立刻MV 面向音乐人、内容创作者和品牌团队,帮助用户低成本完成歌曲 MV、歌词视频、AI 分镜、图生视频与最终合成。

      蛙蛙写作 网页版
      蛙蛙写作 网页版

      蛙蛙写作 AI 创作工具,覆盖小说、剧本、文案,一键生成文稿,打通文字转视频全流程,高效解决创作难题。

      讯飞智作 网页版
      讯飞智作 网页版

      讯飞智作是一款基于语音合成技术、虚拟人驱动技术与数字人形象声音构建能力的 AI 数字人音视频创作平台。

      超级简历WonderCV 网页版
      超级简历WonderCV 网页版

      超级简历WonderCV是面向应届生和AI小白的智能求职平台,覆盖简历、面试与职业规划。

      音述AI 网页版
      音述AI 网页版

      音述AI,是一个以“用声音述说故事”为核心的 AI 音乐创作与声音分享社区。

      AionClaw macOS版
      AionClaw macOS版

      AionClaw 是可在桌面端运行的任务型 AI 助手,通过数字专家与技能市场组合办公、创作、数据和开发能力。

      Loomy macOS版
      Loomy macOS版

      Loomy 是科大讯飞推出的桌面 AI 助手,可通过对话、任务与工作目录协助处理办公、内容创作和资料整理。

      AirBuddy 3 macOS版
      AirBuddy 3 macOS版

      AirBuddy 3 是面向 macOS 的蓝牙外设增强工具,可集中查看 AirPods、Beats 与苹果设备电量,提供快速连接、播放控制、电量提醒、桌面小组件和多台 Mac 外设接力等功能。

      AirServer macOS版
      AirServer macOS版

      AirServer macOS版可把 Mac 转换为无线投屏接收端,通过 AirPlay 或 Google Cast 接收手机、平板和电脑画面,并支持多设备同时连接、高清镜像及会话录制,适合家庭娱乐、课堂教学、会议演示和应用展示。

      AnyDesk macOS版
      AnyDesk macOS版

      AnyDesk macOS版是一款适用于远程办公、设备维护和客户支持的远程桌面工具,可连接并操作异地计算机,支持文件传输、无人值守访问、会话权限控制及跨平台协作。

      WINDOWS 更多
      讯飞绘文 网页版
      讯飞绘文 网页版

      讯飞绘文网页版是科大讯飞推出的AI图文创作与内容运营工具,覆盖热点选题、文章生成、稿件校对、智能配图、排版发布和数据复盘等环节,适合自媒体创作者及内容团队在线使用。

      Maya(Autodesk Maya) Windows版
      Maya(Autodesk Maya) Windows版

      Autodesk Maya 是面向影视、电视、动画和游戏行业的专业三维制作软件,集多边形与 NURBS 建模、角色绑定、关键帧和动作捕捉动画、Bifrost 特效模拟、材质开发及 Arnold 渲染于一体,

      Aperty Windows版
      Aperty Windows版

      Aperty 是 Skylum 推出的 AI 人像修图软件,支持自然肤质修饰、瑕疵处理、面部与身体调整、预设及批量同步。

      火山引擎 网页版
      火山引擎 网页版

      火山引擎是字节跳动旗下企业级云与AI服务平台。聚焦大模型核心能力,依托字节自研豆包多模态大模型,凭借火山方舟一站式平台,提供模型精调、推理部署、智能应用搭建等全链路服务,结合高性能AI算力,助力各行业快速落地大模型数字化与智能化应用。

      立刻MV 网页版
      立刻MV 网页版

      立刻MV 面向音乐人、内容创作者和品牌团队,帮助用户低成本完成歌曲 MV、歌词视频、AI 分镜、图生视频与最终合成。

      蛙蛙写作 网页版
      蛙蛙写作 网页版

      蛙蛙写作 AI 创作工具,覆盖小说、剧本、文案,一键生成文稿,打通文字转视频全流程,高效解决创作难题。

      讯飞智作 网页版
      讯飞智作 网页版

      讯飞智作是一款基于语音合成技术、虚拟人驱动技术与数字人形象声音构建能力的 AI 数字人音视频创作平台。

      超级简历WonderCV 网页版
      超级简历WonderCV 网页版

      超级简历WonderCV是面向应届生和AI小白的智能求职平台,覆盖简历、面试与职业规划。

      音述AI 网页版
      音述AI 网页版

      音述AI,是一个以“用声音述说故事”为核心的 AI 音乐创作与声音分享社区。

      AionClaw Windows版
      AionClaw Windows版

      AionClaw 是可在桌面端运行的任务型 AI 助手,通过数字专家与技能市场组合办公、创作、数据和开发能力。

      Loomy Windows版
      Loomy Windows版

      Loomy 是科大讯飞推出的桌面 AI 助手,可通过对话、任务与工作目录协助处理办公、内容创作和资料整理。

      AirServer Windows版
      AirServer Windows版

      AirServer Windows版可把电脑变成AirPlay、Google Cast和Miracast无线投屏接收端,适合会议演示、课堂教学及家庭大屏分享。发送设备通常无需另装客户端,并可同时接收和智能排列多个镜像会话。