datetime.strftime
时间: 2023-11-06 18:07:17 浏览: 87
(datetime format)
The strftime() method in Python is used to convert a datetime object to a string representation of the date and time. It takes a format string as an argument that specifies how the output string should be formatted.
The format string contains special characters that are replaced with the corresponding date and time values. For example, %Y is replaced with the year in four digits, %m with the month in two digits, %d with the day in two digits, %H with the hour in two digits, %M with the minute in two digits, and %S with the second in two digits.
Here is an example of how to use the strftime() method to format a datetime object:
```python
import datetime
now = datetime.datetime.now()
formatted_date = now.strftime("%Y-%m-%d %H:%M:%S")
print("Formatted Date:", formatted_date)
```
This will output the current date and time in the format "YYYY-MM-DD HH:MM:SS". You can customize the format string according to your needs.
阅读全文