Python標準庫datetime之datetime模塊用法分析詳解
1、日期時間對象
- 日期時間對象是指具有日期(年月日)和時間(時分秒)雙重屬性的實例
- 日期時間對象的類型為datetime.datetime
- 日期時間對象常用的屬性有年、月、日、時、分、秒、微秒
- 日期時間對象可以指定時間創(chuàng)建,也可以通過獲取當前時間來創(chuàng)建
- 日期時間對象指定時間創(chuàng)建時可按位置傳參創(chuàng)建,也可關鍵字傳參創(chuàng)建
- 日期時間對象的創(chuàng)建函數(shù)有datetime.datetime(),datetime.datetime.now()、datetime.datetime.today()、datetime.datetime.utcnow()
- 日期時間對象通過datetime.datetime()創(chuàng)建時的參數(shù)依次為:year,month,day,hour,minute,second,microsecond
- 日期時間對象通過datetime.datetime.now()函數(shù)創(chuàng)建不需要參數(shù)
- 日期時間對象通過datetime.datetime.today()函數(shù)創(chuàng)建不需要參數(shù)
- 日期時間對象通過datetime.datetime.utcnow()函數(shù)創(chuàng)建不需要參數(shù)
- 日期時間對象通過datetime.datetime()創(chuàng)建時至少應該包含年、月、日三個參數(shù)
- 日期時間對象通過datetime.datetime()創(chuàng)建時的參數(shù)范圍如下
序號 | 形參 | 實參范圍 |
---|---|---|
1 | year | 1~9999 |
2 | month | 1~12 |
3 | day | 0~23 |
4 | hour | 0~23 |
5 | minute | 0~59 |
6 | second | 0~59 |
7 | microsecond | 1~999999 |
2、創(chuàng)建日期時間對象
2.1、通過datetime.datetime.utcnow()創(chuàng)建
datetime_zero = datetime.datetime.utcnow()
2.2、通過datetime.datetime.today()函數(shù)創(chuàng)建
datetime_first = datetime.datetime.today()
2.3、通過datetime.datetime.now()創(chuàng)建
datetime_second = datetime.datetime.now()
2.4、通過datetime.datetime()創(chuàng)建
- 指定日期時間創(chuàng)建
- 必傳年、月、日參數(shù)
- 指定日期時間、位置參數(shù)的順序不可變且參數(shù)值必須在規(guī)定的范圍內(nèi)
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、查看創(chuàng)建的對象
print(datetime_zero, type(datetime_zero)) # 2022-07-09 18:12:43.486469 <class 'datetime.datetime'> print(datetime_first, type(datetime_first)) # 2022-07-09 18:12:43.486469 <class 'datetime.datetime'> print(datetime_second, type(datetime_second)) # 2022-07-09 18:12:43.486469 <class 'datetime.datetime'> print(datetime_three, type(datetime_three)) # 0001-01-01 00:00:00.000001 <class 'datetime.datetime'> print(datetime_four, type(datetime_four)) # 9999-12-31 23:59:59.999999 <class 'datetime.datetime'> print(datetime_five, type(datetime_five)) # 9999-12-31 23:59:59.999999 <class 'datetime.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
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 <class 'int'> """# 從日期時間對象中獲取月份""" datetime_month = datetime_first.month print(datetime_month, type(datetime_month)) # 7 <class 'int'> """# 從日期時間對象中獲取天""" datetime_day = datetime_first.day print(datetime_day, type(datetime_day)) # 10 <class 'int'> """# 從日期時間對象中獲取小時""" datetime_hour = datetime_first.hour print(datetime_hour, type(datetime_hour)) # 18 <class 'int'> """# 從日期時間對象中獲取分鐘""" datetime_minute = datetime_first.minute print(datetime_minute, type(datetime_minute)) # 56 <class 'int'> """# 從日期時間對象中獲取秒數(shù)""" datetime_second = datetime_first.second print(datetime_second, type(datetime_second)) # 16 <class 'int'> """# 從日期時間對象中獲取微秒""" datetime_microsecond = datetime_first.microsecond print(datetime_microsecond, type(datetime_microsecond)) # 735264 <class 'int'>
“”“# datetime.datetime.date()函數(shù)的參數(shù)只能是datetime.datetime類型”“”
date_time = datetime.date(2022, 12, 26)“”“# 傳入的參數(shù)不能為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)
“”“# 傳入的參數(shù)不能為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()函數(shù)傳入的參數(shù)亦不能為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))”""
4、日期時間對象轉換為時間元組
- 時間元組是指具有 年份、月份、日、小時、分鐘、秒數(shù)、星期中的第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)) # 類型為:<class 'time.struct_time'>
5、將日期時間對象轉化為公元歷開始計數(shù)的天數(shù)
- 日期時間對象轉化為公元歷開始計數(shù)的天數(shù)
- 將一個整形數(shù)值轉換為日期時間對象
- 整形數(shù)值最大值為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 <class 'int'> 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))
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)) ```  ## 7、日期時間對象轉換為時間戳 ```python datetime_timestamp = datetime.datetime.timestamp(datetime_replace) print(datetime_timestamp, type(datetime_timestamp)) # 1657365267.000123 <class 'float'> ```  ## 8、時間戳轉換為日期時間對象 ```python print(datetime.datetime.fromtimestamp(datetime_timestamp)) # 2022-07-09 19:14:27.000123 ```  ## 9、日期時間對象轉換為時間元組 ```python datetime_timetuple = datetime.datetime.timetuple(datetime_replace) print(datetime_timetuple, type(datetime_timetuple)) ```  ## 10、ISO標準日期時間格式 ISO標準的日歷時間,Calendar中文釋義為日歷 * 各個值的含義為(年份、周數(shù)、周內(nèi)的第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 <class 'str'> # 將ISO標準日期時間格式的字符串轉換為日期時間類型 From_UTC_ISO_DateTime = datetime.datetime.fromisoformat('9999-12-31T23:59:59.999999') # <class 'datetime.datetime'> print(From_UTC_ISO_DateTime, type(From_UTC_ISO_DateTime)) # ISO標準的周內(nèi)第N天 # 值的范圍是[1,7],1代表周一,7代表周日 UTC_ISO_WeekDateTime = datetime.datetime.isoweekday(UTCDateTime) print(UTC_ISO_WeekDateTime, type(UTC_ISO_WeekDateTime)) # 7 <class 'int'> # ISO標準的日歷時間,Calendar中文釋義為日歷 # 各個值的含義為(年份、周數(shù)、周內(nèi)的第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)) # <class 'datetime.datetime'> ```  ## 11、日期時間替換函數(shù)replace() * replace()可以只替換日期時間屬性的某一項 * replace()函數(shù)的第一個參數(shù)必傳 * replace()函數(shù)的第一個參數(shù)是一個日期時間類型(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)}") # 不傳參數(shù) 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 # 只替換月份, 替換其他參數(shù)同理 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 # 替換其他參數(shù)同理 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 ```  ## 12、日期時間對象格式化strftime() * 日期時間對象格式化常用的格式如下 * %H(兩位數(shù)的小時) * %M(兩位數(shù)的分鐘) * %S(兩位數(shù)的秒) * %f(6位數(shù)的微秒) * %h(簡寫的月份名,一般為英文簡寫) * %y(兩位數(shù)的年份) * %Y(四位數(shù)的年份) * %m(兩位數(shù)的月份) * %d(兩位數(shù)的天數(shù)) * 可以只格式化部分屬性 ```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 <class 'str'> # 格式化日期屬性 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 # coding:utf-8 import datetime # 日期時間對象 # 日期時間對象是指具有日期(年月日)和時間(時分秒)雙重屬性的實例 # 日期時間對象的類型為datetime.datetime # 日期時間對象常用的屬性有年、月、日、時、分、秒、微秒等 # 日期時間對象可以指定時間創(chuàng)建,也可以通過獲取當前時間來創(chuàng)建 # 日期時間對象指定時間創(chuàng)建時可按位置傳參創(chuàng)建,也可關鍵字傳參創(chuàng)建 # 日期時間對象的創(chuàng)建函數(shù)有datetime.datetime(),datetime.datetime.now()、datetime.datetime.today()、datetime.datetime.utcnow() # 日期時間對象通過datetime.datetime()創(chuàng)建時的參數(shù)依次為:year,month,day,hour,minute,second,microsecond # 日期時間對象通過datetime.datetime.now()函數(shù)創(chuàng)建不需要參數(shù) # 日期時間對象通過datetime.datetime.today()函數(shù)創(chuàng)建不需要參數(shù) # 日期時間對象通過datetime.datetime.utcnow()函數(shù)創(chuàng)建不需要參數(shù) # 日期時間對象通過datetime.datetime()創(chuàng)建時至少應該包含年月日三個參數(shù) # 日期時間對象通過datetime.datetime()創(chuàng)建時的參數(shù)范圍如下 # {year[1~9999]、month[1~12]、day[1~31]、hour[0~23]、minute[0~59]、second[0~59]、microsecond[1~999999]} # 通過datetime.datetime.utcnow()創(chuàng)建 datetime_zero = datetime.datetime.utcnow() # 通過datetime.datetime.today()函數(shù)創(chuàng)建 datetime_first = datetime.datetime.today() # 通過datetime.datetime.now()創(chuàng)建 datetime_second = datetime.datetime.now() # 通過datetime.datetime()函數(shù)指定日期時間、關鍵字傳參創(chuàng)建 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()函數(shù)指定日期時間、按位置傳參創(chuàng)建,順序不可變且參數(shù)值必須在規(guī)定的范圍內(nèi) datetime_five = datetime.datetime(9999, 12, 31, 23, 59, 59, 999999) print(datetime_zero, type(datetime_zero)) # 2022-07-09 18:12:43.486469 <class 'datetime.datetime'> print(datetime_first, type(datetime_first)) # 2022-07-09 18:12:43.486469 <class 'datetime.datetime'> print(datetime_second, type(datetime_second)) # 2022-07-09 18:12:43.486469 <class 'datetime.datetime'> print(datetime_three, type(datetime_three)) # 0001-01-01 00:00:00.000001 <class 'datetime.datetime'> print(datetime_four, type(datetime_four)) # 9999-12-31 23:59:59.999999 <class 'datetime.datetime'> print(datetime_five, type(datetime_five)) # 9999-12-31 23:59:59.999999 <class 'datetime.datetime'> # 查看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 <class 'int'> """# 從日期時間對象中獲取月份""" datetime_month = datetime_four.month print(datetime_month, type(datetime_month)) # 12 <class 'int'> """# 從日期時間對象中獲取天""" datetime_day = datetime_four.day print(datetime_day, type(datetime_day)) # 31 <class 'int'> """# 從日期時間對象中獲取小時""" datetime_hour = datetime_four.hour print(datetime_hour, type(datetime_hour)) # 23 <class 'int'> """# 從日期時間對象中獲取分鐘""" datetime_minute = datetime_four.minute print(datetime_minute, type(datetime_minute)) # 59 <class 'int'> """# 從日期時間對象中獲取秒數(shù)""" datetime_second = datetime_four.second print(datetime_second, type(datetime_second)) # 59 <class 'int'> """# 從日期時間對象中獲取微秒""" datetime_microsecond = datetime_four.microsecond print(datetime_microsecond, type(datetime_microsecond)) # 999999 <class 'int'> """# datetime.datetime.date()函數(shù)的參數(shù)只能是datetime.datetime類型""" date_time = datetime.date(2022, 12, 26) """# 傳入的參數(shù)不能為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) """# 傳入的參數(shù)不能為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()函數(shù)傳入的參數(shù)亦不能為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))""" # 將日期時間對象轉換為時間元組類型 # 時間元組是指具有 年份、月份、日、小時、分鐘、秒數(shù)、星期中的第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)) # 類型為:<class 'time.struct_time'> # 將日期時間對象轉化為公元歷開始計數(shù)的天數(shù) 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 <class 'int'> 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 <class 'float'> # 將時間戳轉換為日期時間對象 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 <class 'str'> # 將ISO標準日期時間格式的字符串轉換為日期時間類型 From_UTC_ISO_DateTime = datetime.datetime.fromisoformat('9999-12-31T23:59:59.999999') # <class 'datetime.datetime'> print(From_UTC_ISO_DateTime, type(From_UTC_ISO_DateTime)) # ISO標準的周內(nèi)第N天 # 值的范圍是[1,7],1代表周一,7代表周日 UTC_ISO_WeekDateTime = datetime.datetime.isoweekday(UTCDateTime) print(UTC_ISO_WeekDateTime, type(UTC_ISO_WeekDateTime)) # 7 <class 'int'> # ISO標準的日歷時間,Calendar中文釋義為日歷 # 各個值的含義為(年份、周數(shù)、周內(nèi)的第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)) # <class 'datetime.datetime'> # 日期時間替換函數(shù)replace() # replace()可以只替換日期時間屬性的某一項 # replace()函數(shù)的第一個參數(shù)必傳 # replace()函數(shù)的第一個參數(shù)是一個日期時間類型(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)}") # 不傳參數(shù) 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 # 只替換月份, 替換其他參數(shù)同理 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 # 替換其他參數(shù)同理 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(兩位數(shù)的小時)、%M(兩位數(shù)的分鐘)、%S(兩位數(shù)的秒)、%f(6位數(shù)的微秒)、%h(簡寫的月份名,一般為英文簡寫) %y(兩位數(shù)的年份)、%Y(四位數(shù)的年份)、%m(兩位數(shù)的月份)、%d(兩位數(shù)的天數(shù)) """ # 可以只格式化部分屬性 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 <class 'str'> # 格式化日期屬性 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模塊用法分析詳解的文章就介紹到這了,更多相關Python datetime模塊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python實現(xiàn)解析命令行參數(shù)的常見方法總結
除ide的執(zhí)行方式外,命令行的方式執(zhí)行Python腳本是參數(shù)化程序執(zhí)行的一種常見且簡單的方法。本文總結了三個常見的獲取和解析命令行參數(shù)的方法,需要的可以參考一下2022-10-10卸載tensorflow-cpu重裝tensorflow-gpu操作
這篇文章主要介紹了卸載tensorflow-cpu重裝tensorflow-gpu操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06