How do i convert a string to a date and time in python?

How do i convert a string to a date and time in python?
Educative Answers Team

Python provides the strptime() method, in its datetime class, to convert a string representation of the​ date/time into a date object.

Syntax

The syntax for the strptime() method is:

Code

The code snippet below illustrates the usage of the strptime() method in Python:

from datetime import datetime

date_time_str = '18/09/19 01:55:19'

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


print ("The type of the date is now",  type(date_time_obj))
print ("The date is", date_time_obj)

The strptime() method will not work if the string argument is not consistent with the format parameter. This is shown below​:

from datetime import datetime

date_time_str = '180919 015519'

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


print ("The type of the date is now",  type(date_time_obj))
print ("The date is", date_time_obj)

How do i convert a string to a date and time in python?

The strptime() method is available under datetime and time modules to parse the string to datetime and time objects.

To convert string to datetime in Python, use the strptime() method. The strptime() is a built-in function of the Python datetime class used to convert a string representation of the​ date/time to a date object.

Syntax

datetime.strptime(date_string, format)

Parameters

The strptime() function takes both the arguments that are mandatory and should be a string. The strptime() function is exactly the opposite of the strftime() function, which converts a datetime object to a string.

Example

# app.py

from datetime import datetime

dt_str = '27/10/20 05:23:20'

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

print("The type of the date is now",  type(dt_obj))
print("The date is", dt_obj)

Output

The type of the date is now 
The date is 2020-10-27 05:23:20

The datetime.strptime() is a general method for parsing strings into datetimes. It can handle all sorts of formats, with the format defined by the format string you give.

Convert Python String to datetime using dateutil

The dateutil can be installed from PyPI using the pip package manager.

To convert String to datetime, import the dateutil package and use the parser module.

# app.py

from dateutil import parser

dt_str = '27/10/20 05:23:20'

dt_obj = parser.parse(dt_str)

print("The type of the date is now",  type(dt_obj))
print("The date is", dt_obj)

Output

The type of the date is now 
The date is 2020-10-27 05:23:20

Python String to date object

To convert string to date object in Python, use the date() function along with the strptime() function.

# app.py

from datetime import datetime

date_str = '10-27-2020'

dto = datetime.strptime(date_str, '%m-%d-%Y').date()
print(type(dto))
print(dto)

Output


2020-10-27

The strptime() function will not work if the string argument is not consistent with the format parameter.

Convert String to Datetime with locale

To set a locale in Python, import the locale package in your Python progrThen, The locale.setlocale() method is used to set the locale.

# app.py

import locale
from datetime import datetime

locale.setlocale(locale.LC_ALL, 'es_ES')
date_str_es = '27-Octubre-2020'  # es_ES locale
datetime_object = datetime.strptime(date_str_es, '%d-%B-%Y')
print(datetime_object)

Output

2020-10-27 00:00:00

That is it for converting a string to datetime in a Python article.

See also

Python Date Format

Python date_range()

Python to_datetime()

Python convert datetime to String

Can we convert string to date in Python?

Python provides the strptime() method, in its datetime class, to convert a string representation of the​ date/time into a date object.

How do I convert a string to a date?

Java String to Date Example.
import java.text.SimpleDateFormat;.
import java.util.Date;.
public class StringToDateExample1 {.
public static void main(String[] args)throws Exception {.
String sDate1="31/12/1998";.
Date date1=new SimpleDateFormat("dd/MM/yyyy").parse(sDate1);.
System.out.println(sDate1+"\t"+date1);.

How do you convert string to time?

You can use the following code for changing the String value into the time equivalent: String str = "08:03:10 pm"; DateFormat formatter = new SimpleDateFormat("hh:mm:ss a"); Date date = (Date)formatter. parse(str);

How do I convert a value to a date in Python?

Example 15: Format date using strftime().
%Y - year [0001,..., 2018, 2019,..., 9999].
%m - month [01, 02, ..., 11, 12].
%d - day [01, 02, ..., 30, 31].
%H - hour [00, 01, ..., 22, 23..
%M - minute [00, 01, ..., 58, 59].
%S - second [00, 01, ..., 58, 59].