strftime
用于格式时间,反馈时间类型有自己定义的fmt 决定的
看下他的源码
def strftime(self, fmt: str) -> str: ...
里面就一个fmt,它的使用就是已知一个时间,然后使用strftime格式化时间。
strptime 也是格式化时间
看下他的源码
- def strptime(string, format): # real signature unknown; restored from __doc__
- """
- strptime(string, format) -> struct_time
-
- Parse a string to a time tuple according to a format specification.
- See the library reference manual for formatting codes (same as
- strftime()).
-
- Commonly used format codes:
-
- %Y Year with century as a decimal number.
- %m Month as a decimal number [01,12].
- %d Day of the month as a decimal number [01,31].
- %H Hour (24-hour clock) as a decimal number [00,23].
- %M Minute as a decimal number [00,59].
- %S Second as a decimal number [00,61].
- %z Time zone offset from UTC.
- %a Locale's abbreviated weekday name.
- %A Locale's full weekday name.
- %b Locale's abbreviated month name.
- %B Locale's full month name.
- %c Locale's appropriate date and time representation.
- %I Hour (12-hour clock) as a decimal number [01,12].
- %p Locale's equivalent of either AM or PM.
-
- Other codes may be available on your platform. See documentation for
- the C library strftime function.
- """
- return struct_time
这个就是里面一个string ,一个定义的fmt
- import datetime
- import time
- now = datetime.datetime.now()
- print(now)
- date1 = now.strftime("%Y-%m-%d %H:%M:%S")
- print(date1)
- date2 = time.strptime("2023-11-15 17:24:20", "%Y-%m-%d %H:%M:%S")
- print(date1)
打印结果
2023-11-17 16:17:15.858824
2023-11-17 16:17:15
2023-11-17 16:17:15