strftime函数是Python中用于格式化日期和时间的函数,可以将日期和时间对象转换为特定格式的字符串。以下是一些strftime函数在时间处理中的技巧:
格式化日期和时间:import datetimenow = datetime.datetime.now()formatted_date = now.strftime("%Y-%m-%d %H:%M:%S")print(formatted_date)输出结果类似于:2021-10-26 14:30:00
获取星期几:weekday = now.strftime("%A")print(weekday)输出结果为星期几的全称,比如:Tuesday
自定义格式化字符串:custom_format = now.strftime("Today is %A, %B %d, %Y. The time is %I:%M %p.")print(custom_format)输出结果为类似于:Today is Tuesday, October 26, 2021. The time is 02:30 PM.
将时间戳转换为日期时间字符串:timestamp = 1635225000date_time = datetime.datetime.fromtimestamp(timestamp).strftime("%Y-%m-%d %H:%M:%S")print(date_time)输出结果类似于:2021-10-26 14:30:00


