import datetime date_time_str = '2018-06-29 08:15:27.243860' date_time_obj = datetime.datetime.strptime(date_time_str, '%Y-%m-%d %H:%M:%S.%f') print('Date:', date_time_obj.date()) print('Time:', date_time_obj.time()) print('Date-time:', date_time_obj)
Here is what the above code is Doing:
1. We first create a string variable that contains a date and time.
2. We then use the datetime.datetime.strptime() method to parse the string into a datetime object.
3. We then print the date, time, and datetime object.