convert string to date in python-yyyy-mm-dd

convert string to date in python : 

In this article we see different methods to show How to conver string to date in python.
Different methods to convert string to date :
1) string to datetime
2) string to date object
3) string to time object
4)  python time strptime()example
before going towards this post If you want to learn free python then click on python tutorials point.

Using strptime function you can convert string to date object in python. we can Provide the date string and the formats in which the date is specified.

convert string to date in python-yyyy-mm-dd


import datetime
datetime_str = '21/06/2020' # The date - 21 jun 2020
formattime_str = '%d/%m/%Y' # The format of date
datetime_format = datetime.datetime.strptime(datetime_str, formattime_str)


This will give the output as follow −


String to datetime
from datetime import datetime

dates_str = '06/21/20 14:55:26'

dates_object = datetime.strptime(dates_str, '%m/%d/%y %H:%M:%S')

print(dates_object) # printed in default formats


<class 'datetime.datetime'>
2020-06-21 14:55:26

python string to date yyyy-mm-dd

String to date object

Use date() functions alongwith strptime() function to convert string to data objects.

date_str = '06-21-2020'

datetime_object = datetime.strptime(date_str, '%m-%d-%Y').date()
print(datetime_object) # printed in default formats


<class ''>

convert string to datetime python pandas

String to time object

 using time() functions alongwith strptime() function to convert string to time objects.

times_str = '14::55::26'
times_object = datetime.strptime(times_str, '%H::%M::%S').time()


<class 'datetime.time'>

Python time strptime() example

Let’s see some example of using the module strptime() functions.

import time

times_obj = time.strptime(time_str, '%H::%M::%S')

# default in the formats - "%a %b %da %H:%M:%S %Y"
print(time.strptime('sun jun 21 15:55:02 2020'))


<class 'time.struct_time'>
time.struct_time(tm_year=1998, tm_mon=1, tm_mday=1, tm_hour=13, tm_min=55, tm_sec=26, tm_wday=0, tm_yday=1, tm_isdst=-1)
time.struct_time(tm_year=2020, tm_mon=6, tm_mday=21, tm_hour=15, tm_min=55, tm_sec=2, tm_wday=2, tm_yday=262, tm_isdst=-1)

Python strptime() ValueError Examples

using try-expect blocks to catch parsing exception and performs corrective action.

dates_str = '06/21/20 14:55:26'

dates_object = datetime.strptime(dates_str, '%m/%d/%y')
except ValueError as ve:
print('ValueError Raised value:', ve)

time_str = '15::55::26'

time_object = time.strptime(time_str, '%H::%M::%S')
except ValueError as e:
print('ValueErrortime:', e)


ValueError Raised value: unconverted data remains: 13:55:26
ValueErrortime: time data '15::55::26' does not match format '%H::%M::%S'
Tags:-convert string to date in python - date yyyy-mm-dd ,datetime python pandas 
  for similar kind of post click on python tutorials point. and if you like this post then please comment and share.
                                    BEST OF LUCK!!!!!
Next Post »

method vs function python-types of method-class in python

method vs function python : Welcome to my article, In this article we are going to see What is method vs function  in python. before going t...