close

How to Convert String To Datetime In Python?

I Was Working With DateTime But I have One String That Contains DateTime My String Is October 20 2021 12:55 PM But, I need DateTime Format I don’t Want it As a String. So I Think I Should Make TUtorial On It So That Today In This Tutorial, We Are Going To Learn About How to Convert String To Datetime In Python? Let’s Get Started This Tutorial.

How to Convert String To Datetime In Python?

  1. How to Convert String To Datetime In Python?

    To Convert String To Datetime In Python We Are Going To use strptime in This Method. Basically, strptime means string parse time Here Is an Official Document For strptime. Here datetime.strptime is the main routine for parsing strings into date time. Just add String to datetime.strptime and Add Your Output Format DateTime. This is How You Can Convert String to DateTime using strptime.

  2. Convert String To Datetime In Python

    To Convert String To Datetime In Python In This Method, We are Going to Use dateutil library First Of All You Just Need to Install dateutil library. Just Run This Command In your terminal. Then Import the parser from dateutil. Now, You can Use parse From the parser Just add String into Parse And It will Give You DateTime Format In OutPut. This is How You can use dateutil.

Method 1: Using strptime

We Are Going To use strptime in This Method. Basically, strptime means string parse time Here Is an Official Document For strptime. Here datetime.strptime is the main routine for parsing strings into date time. Just add String to datetime.strptime and Add Your Output Format DateTime. Here is an example.

from datetime import datetime
datetime_formate = datetime.strptime('Oct 20 2021  12:55PM', '%b %d %Y %I:%M%p')
print(datetime_formate)

Output.

2021-10-20 12:55:00

This is How You Can Convert String to DateTime using strptime.

Method 2: Using dateutil library

In This Method, We are Going to Use dateutil library First Of All You Just Need to Install dateutil library. Just Run This Command In your terminal.

pip install python-dateutil

Then Import the parser from dateutil.

from dateutil import parser

Now, You can Use parse From the parser Just add String into Parse And It will Give You DateTime Format In OutPut.

from dateutil import parser
datetime_formate = parser.parse("Oct 20 2021 12:55PM")
print(datetime_formate)

Output:

2021-10-20 12:55:00

This is How You can use dateutil.

Method 3: Using datetime.fromisoformat

First Of all Import datetime from datetime Just like the below snippet.

from datetime import datetime

Now, datetime has fromisoformat where you can pass a string and it will give you output as DateTime Format. Here is My Example.

from datetime import datetime

stringof_datetime = "2021-10-20 12:55"
print (datetime.fromisoformat(stringof_datetime))

Output

2021-10-20 12:55:00

Conlusion

Hope all 3 Method Are Useful For You. Comment Below Which Method You Used To Convert String To Datetime In Python. Thank You.

Also Read

Leave a Comment