Convert string to datetime python

Convert string to datetime python : We convert the string to datetime using the strptime () function. The function is available in dateTime and the time module to parse a string to datetime and time object. From dateutil.parser import parse
Parse(‘9 aril 201110:30’)
Datetime.datetime (2011, 4, 9, 10, 30)
Parse(‘Mar 3,7pm’)
Datetime.datetime (2011, 3, 3, 19, 0)
We install it (use easy_install or pip). Python,
F=”%d/%m%Y%H: %M: %S”
D_str=’01/04/2011 16:08:18’
Datetime.datetime.strptime (d_str, f)
Datetime.datetime (2011, 4, 1, 16, 8, 18)
It is easy to deal with what we use very easily.
The datetime module in the Python programming standard library has two functions as strftime and strptime.
 The datetime objects into strings and parse strings into datetime objects.

Example -

import datetime
D=datetime.datetime.now ()
D
Datetime.datetime (2011, 4, 1, 16, 8, 18, 445529)
F=”%d%m%Y%H: %M: %S”
D.strftime (f)
‘01/04/2011 16:08:18’
D_str=’01/04/2011 16:08:18’
Datetime.datetime.strptime (d_str,f)
Datetime.datetime (2011, 4, 1, 16, 8, 18)

String to datetime:-

From datetime import datetime
Datetime_str=’09/19/18 13:55:26’
Datetime_object=datetime.strptime (datetime_str,’%m%d%y%H: %M: %S’)
Print(type (datetime_object))
Print(datetime_object)
Output:-
2018-09   13:55:26
String to date object:-
We use date () function with strptime () function to convert the string to date object.
Example:-
Datetime_str=’09/19/18’
Datetime_object=datetime.strptime (datetime_str,’%m-%d-%Y’).date ()
Print(type (date_object))
Print(date_object)
Output:-
2018-09-19

String to time object:-

Here we use time () function with strptime () function that converts string to time object.
Example:-
Time_str=’13:: 55:: 26’
time_object=datetime.strptime (time_str,’%H: %M:: %S’).time ()
Print(type (time_object))
Print(time_object)
Output:-
13:55:26

Python Convert String to Datetime with locale:-

The locale-specific string will be converted to datetime object and use locale module to set the locale.
Example:-
Import locale
Locale.setlocale (locale.LC_ALL,’de_DE’)
Date_str_DE=’10-Dezember-2018 Montag’
Datetime_object=datetime.strptime (date_st_de_DE,’%d-%B-%Y%A’)
Print(datetime_object)
Output:-
2018-12-10   00:00:00

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