datetime.datetime.strptime()
datetime.datetime.strptime() is a Python function in the Datetime Module category. Parses a string into a datetime object according to a format string. The syntax is datetime.datetime.strptime(date_string, format). Parameters: date_string, format. Returns: Datetime object. A typical example: dt = datetime.strptime("2024-01-15", "%Y-%m-%d")
print(dt) # 2024-01-15 00:00:00. A close sibling is datetime.date(), which creates a date object representing a specific date (year, month, day). A close sibling is datetime.date.today(), which returns the current local date. A close sibling is datetime.datetime.now(), which returns the current local date and time with optional timezone. A close sibling is datetime.datetime.utcnow(), which returns the current UTC date and time (deprecated, use now(timezone.utc)). More about this category: Date and time handling — date, datetime, timedelta, timezone. For a broader walkthrough, read the Datetime Module guide. Related Datetime Module entries: datetime.date(), datetime.date.today(), datetime.datetime.now(), datetime.datetime.utcnow(), datetime.datetime.fromtimestamp(), datetime.datetime.strftime(). This page is part of the free Python Reference documentation covering 978+ functions, methods, and modules with examples and parameter details.