Python convert string to datetime

We can convert the string to datetime using the strptime () function. Here the function is available in datetime to parse datetime and time objects.Convert String to Datetime with locale:-
The string is converted into datetime object as we use the locale module for this conversion.
Example:-
Import locale
Locale.setlocale (locale.LC_all,’de_DE’)
Date_str_de_DE=’10-Dezember-2018 Montag’
Datetime_object=datetime.strptime (date_str_de_DE,’%d-%B-%Y%A’)
Print(datetime_object)
Output:-
2018-12-10 00:00:00

Convert string to DateTime:-

We should write python program to convert the string using strptime() function and result is as follows.
Example:-
Input: dec 4 2018 10:07AM
Output:-2018-12-04 10:07:00

Input: June 12 2013 5:30PM
Output:-2013-06-12 17:30:00

The strptime () function is available in datetime and the time module is used for Date-Time Conversion.
 This function will change the string of datetime into the desired format.
Syntax:-
Datetime.strpttime (date_string, format)

Example:-

import datetime 
def convert (date_time):
format = '%b %d %Y %I: %M%p'
datetime_str = datetime.datetime.strptime (date_time, format)
 return datetime_str
date_time = 'Dec 4 2018 10:07AM'
print(convert (date_time))
Output:-
2018-12-04 10:07:00

Use the datetime.strptime to reverse conversion and get datetime object.
The f and p are methods as format and parse.
It will parse the input string with datetime and return datetime object.
Example:-
From datetime import datetime
Datetime.strptime (“2018-01-31”,”%Y-%m-%d”)
Output:-
Datetime.datetime (2018, 1, 31, 0, 0)
Using datetime library:-
String is the’06-02-2018’

Example:-


From datetime import datetime
Date_string=’ 06-02-2018’
           date_object = datetime.strptime (date_string, '%d-%m-%Y')
print (date_object)
Output:-
2018-02-06 00:00:00

Where 00:00:00 will represents time,
%d = day in 2 digit,
%m = month in 2 digit,
%Y = year in 4 digit,

2. Using external library dateutil  parser:-

Here we first install it by opening Terminal /Command prompt and type
‘pip install python-dateutil’
then press enter key.
Example:-
from dateutil import parser
date_string = 'feb 06 2018 08:00PM'
date_object = parser.parse(date_string)
print(date_object)
Output:-
2018-02-06 20:00:00

By using external library timestring

Again to use this library we have to install it. To install it open your terminal or command prompt and type
‘pip install timestring’
After installation is done, now we can use this library in our program.


1
2
3
4
5

import timestring
date_string = 'feb 06 2018 08:00PM'
date_object = timestring.Date(date_string)
print(date_object)

Output
2018-02-06 20:00:00
Again here we don’t need to write directives. for more detail on timestring library go to https://github.com/stevepeak/timestring

4. Using external library dateparser

To install dateparser, open terminal or command prompt and enter following command.
‘pip install dateparser’


1
2
3
4

import dateparser
date_string = '02/06/2018'
date_object = dateparser.parse(date_string)
print(date_object)

Output
2018-02-06 00:00:00
And if our date string is ‘Fri, 06 feb 2018 08:55:00’ then the program will be same as above, just replace the date_string.
You can ask your queries related to python convert string to datetime.

Additional Services : Refurbished Laptops Sales, Python Classes, Share Market Classes And SEO Freelancer in Pune, India