convert string to date in python 3.10

Spread the love

convert string to date in python 3.10

In this article we see different methods to show How to convert 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. https://www.python.org/
convert string to date in python
fig 01)convert string to date in python-yyyy-mm-dd
 Example
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)

print(datetime_format.date())

Output

This will give the output as follow −
2020-06-21
 
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(type(dates_object))
 
print(dates_object) # printed in default formats
 
Output:
 
<classdatetime.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(type(datetime_object))
 
print(datetime_object) # printed in default formats
 
Output:
<classdatetime.date‘>
 
2020-06-21
 

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()
 
print(type(times_object))
 
print(times_object)
 
Output:
 
<classdatetime.time‘>
 
14:55:26
 

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’)
 
print(type(times_obj))
 
print(times_obj)
 

 
# default in the formats – “%a %b %da %H:%M:%S %Y”
 
print(time.strptime(‘sun jun 21 15:55:02 2020’))
 
Output:
<classtime.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′
 

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

 
time_str = ’15::55::26′
 

 
try:
 
time_object = time.strptime(time_str, ‘%H::%M::%S’)
 
except ValueError as e:
 
print(‘ValueErrortime:’, e)
 
Output:
 
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!!!!!
pagarsach14@gmail.com

sachin Pagar

I am Mr. Sachin pagar Embedded software engineer, the founder of Sach Educational Support(Pythonslearning), a Passionate Educational Blogger and Author, who love to share the informative content on educational resources.

Leave a Reply