The time method in python generates the current time year, month, day, hour, minute and second

In Python, you can use the strftime() method in the time module combined with the time format string to generate the current year, month, day, hour, minute and second. Here is a sample code with a detailed explanation:

import time
 # 获取当前时间的时间戳
current_timestamp = time.time()
 # 将时间戳转换为本地时间的struct_time对象
local_time = time.localtime(current_timestamp)
 # 使用strftime()方法将struct_time对象格式化为指定的时间字符串
current_time = time.strftime("%Y-%m-%d %H:%M:%S", local_time)
 # 输出当前的年月日时分秒
print(current_time)


         In the above code, first use the time.time() method to get the current timestamp. Then, use the time.localtime() method to convert the timestamp into a struct_time object in local time. Next, use the time.strftime() method to format the struct_time object according to the specified time format string "%Y-%m-%d %H:%M:%S" to generate the current year, month, day, hour and minute String representation of seconds. Finally, the current year, month, day, hour, minute and second are output through the print statement.
 Please note that the symbols in the time format string have the following meanings:
- "%Y": four-digit year (for example: 2022)
- "%m": two-digit month (01 to 12)
- "% d": two-digit date (01 to 31)
- "%H": two-digit hour (00 to 23)
- "%M": two-digit minute (00 to 59)
- "%S" : Two-digit seconds (00 to 59)
 By combining different symbols, different formats of time strings can be generated as needed.

Guess you like

Origin blog.csdn.net/weixin_49786960/article/details/131674203