1import datetime
2
3date_time_str = '2018-06-29 08:15:27.243860'
4date_time_obj = datetime.datetime.strptime(date_time_str, '%Y-%m-%d %H:%M:%S.%f')
5
6print('Date:', date_time_obj.date())
7print('Time:', date_time_obj.time())
8print('Date-time:', date_time_obj)
9
1from datetime import datetime
2
3datetime_str = '09/19/18 13:55:26'
4
5datetime_object = datetime.strptime(datetime_str, '%m/%d/%y %H:%M:%S')
6
7print(type(datetime_object))
8print(datetime_object) # printed in default format
9
1date_str = '09-19-2018'
2
3date_object = datetime.strptime(date_str, '%m-%d-%Y').date()
4print(type(date_object))
5print(date_object) # printed in default formatting
6
1from datetime import datetime
2
3datetime_object = datetime.strptime('Jun 1 2005 1:33PM', '%b %d %Y %I:%M%p')
1import datetime
2
3# str value "Apr 2, 2019" convert into any format.
4datetime.datetime.strptime('Apr 2, 2019', '%b %d, %Y').strftime('%a, %d %b %Y')