[Python] strptime(), strftime()
strptime(), strftime()
How did I encounter this?
The user input of the purchase date of a game is in str, which should be parsed into datetime type. I found out there are 2 methods from datetime class for this.
Syntax
strptime()
"String Parse Time": it converts a string into a datetime object.
example:
from datetime import datetime
date_string = "2022-11-09"
date_object = datetime.strptime(date_string, "%Y-%m-%d")
print(date_object) # Output: 2022-11-09 00:00:00
strftime()
"String Format Time": it converts a datetime object into a string in a specific format.
example:
from datetime import datetime
date_object = datetime(2022, 11, 9)
formatted_date = date_object.strftime("%Y-%m-%d")
print(formatted_date) # Output: 2022-11-09
Comments
Post a Comment