In Python, you can convert a date string (a string representing a date) into a date object by using the datetime.strptime() method.
Syntax:
date_object = strptime(date_string, format).date()
Where:
- date_string: Your input date string
- format: The format string that describes the structure of your date string. This is a combination of format codes (you can find them at the end of this article)
Table Of Contents
Examples
This example turns a date string into a date object:
import datetime
date_string = "2023-30-12"
date_object = datetime.datetime.strptime(date_string, "%Y-%d-%m").date()
print(type (date_object))
Output:
<class 'datetime.date'>
In this example, you’ll get a datetime object (by not calling the date() method as we did in the example above):
import datetime
datetime_string = '2023-06-29 08:15:27.243860'
datetime_object = datetime.datetime.strptime(
datetime_string,
'%Y-%m-%d %H:%M:%S.%f'
)
print(type(datetime_object))
Output:
<class 'datetime.datetime'>
Format codes
Here’re all the legit format codes that you can use with the datetime.strptime() method:
- %a: First three characters of the weekday, e.g., Mon, Tue.
- %A: Full name of a weekday, e.g., Sunday or Monday.
- %w: Weekday as a decimal number, where 0 is Sunday, and 6 is Saturday.
- %d: Day of a month as a zero-padded decimal number (01 to 31).
- %-d: Day of a month as a decimal number. (Platform specific)
- %b: Month as the locale’s abbreviated name.
- %B: Month as the locale’s full name.
- %m: Month as a zero-padded decimal number.
- %-m: Month as a decimal number. (Platform specific)
- %y: Year without century as a zero-padded decimal number.
- %Y: Year with century as a decimal number.
- %H: Hour (24-hour clock) as a zero-padded decimal number.
- %-H: Hour (24-hour clock) as a decimal number. (Platform specific)
- %I: Hour (12-hour clock) as a zero-padded decimal number.
- %-I: Hour (12-hour clock) as a decimal number. (Platform specific)
- %p: Locale’s equivalent of either AM or PM.
- %M: Minute as a zero-padded decimal number.
- %-M: Minute as a decimal number. (Platform specific)
- %S: Second as a zero-padded decimal number.
- %-S: Second as a decimal number. (Platform specific)
- %f: Microsecond as a decimal number, zero-padded on the left.
- %z: UTC offset in the form ±HHMM[SS[.ffffff]] (empty string if the object is naive).
- %Z: UTC time zone name (empty string if the object is naive).
- %j: Day of the year as a zero-padded decimal number.
- %-j: Day of the year as a decimal number. (Platform specific)
- %U: Week number of the year (Sunday as the first day of the week) as a zero-padded integer (00 to 53). All days in a new year preceding the first Sunday are considered to be in week 0.
- %W: Week number of the year (Monday as the first day of the week) as a decimal number. All days in a new year preceding the first Monday are considered to be in week 0.
- %c: Locale’s appropriate date and time representation.
- %x: Locale’s appropriate date representation.
- %X: Locale’s appropriate time representation.
- %%: Literal % character.