With the Python Datetime strptime function, you can convert a string to DateTime. The strptime function is available in DateTime and time modules, you have to import one of them to parse a string to DateTime and time objects respectively.
“strptime = String parse time”
Syntax
Python strptime() is a class method in datetime class.
datetime.datetime.strptime(date_string, format)
OR
Time module has the similar function available, here is syntax
time.strptime(time_string[, format])
Python datetime strptime Example
For a get datetime class, you need to import DateTime module.
import datetime # MM/DD/YY HH:MM:SS datetime_str = '10/11/18 14:35:32' datetime_obj = datetime.datetime.strptime(datetime_str, '%m/%d/%y %H:%M:%S') print(datetime_obj)
Output: 2018-10-11 14:35:32
OR – the Same example for above, import “from module_name.member_name” datetime.
from datetime import datetime # MM/DD/YY HH:MM:SS datetime_str = '10/11/18 14:35:32' datetime_obj = datetime.strptime(datetime_str, '%m/%d/%y %H:%M:%S') print(datetime_obj)
Get the Time from String
Example of get the TIME only.
import datetime time_str = '15:35:36' time_obj = datetime.datetime.strptime(time_str, '%H:%M:%S').time() print(time_obj)
Output: 15:35:36
Get the Date from String
Example of get the DATE only.
import datetime date_str = '10-11-2018' date_object = datetime.datetime.strptime(date_str, '%m-%d-%Y').date() print(date_object)
Output: 2018-10-11
Python time strptime example
You have to import a time module for this example.
import time # MM/DD/YY HH:MM:SS datetime_str = '10/11/18 14:35:32' print(time.strptime(datetime_str, '%m/%d/%y %H:%M:%S'))
Output: time.struct_time(tm_year=2018, tm_mon=10, tm_mday=11, tm_hour=14, tm_min=35, tm_sec=32, tm_wday=3, tm_yday=284, tm_isdst=-1)
Python strptime() format directives
Here is a list of formate directives used in strptime() function.
Directive | Description | Example |
---|---|---|
%a | Short name of weekday | Fri |
%A | Full name of Weekday | Friday |
%w | Weekday as a number 0-6, 0 is Sunday – 0 Monday – 1 … | 5 |
%d | Day of month 01-31 | 01 |
%b | Month name, short version | Oct |
%B | Month name, full version | October |
%m | Month as a number 01-12 | 10 |
%y | Year, short version, without century | 18 |
%Y | Year, full version | 2018 |
%H | Hour 00-23 | 00 |
%I | Hour 00-12 | 12 |
%p | AM/PM | AM |
%M | Minute 00-59 | 38 |
%S | Second 00-59 | 01 |
%f | Microsecond 000000-999999 | 844628 |
%z | UTC offset | +0100 |
%Z | Timezone | UTC+01:00 |
%j | Day number of year 001-366 | 283 |
%U | Week number of year, Sunday as the first day of the week, 00-53 | 40 |
%W | Week number of year, Monday as the first day of the week, 00-53 | 41 |
%c | Local version of date and time | Wed Oct 10 03:38:01 2018 |
%x | Local version of date | 10/10/18 |
%X | Local version of time | 03:38:01 |
%% | A % character | % |
Error:
module ‘datetime’ has no attribute ‘strptime’?
Use 2-time DateTime because datetime is also stood for the module. So you need to access datetime class then strptime function.
import datetime date_str = '10-11-2018' date_object = datetime.datetime.strptime(date_str, '%m-%d-%Y').date()
or use import “from module_name.member_name” datetime.
from datetime import datetime datetime_str = '10/11/18 14:35:32' datetime_obj = datetime.strptime(datetime_str, '%m/%d/%y %H:%M:%S')
Note: This example (Project) is developed in PyCharm 2018.2 (Community Edition)
JRE: 1.8.0
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.13.6Python 3.7
All examples are in Python 3, so it may change its different from python 2 or upgraded versions.