| by Arround The Web | No comments

Converting Python String to Date

In Python, we convert Python strings to dates for storing dates in databases, performing date calculations, and formatting dates. Python delivers several methods to deal with the date and time, for instance, the “datetime” module, “dateutil” module, etc.

How to Convert Python String to Date?

To transform/convert a Python string to a datetime object, the below approaches are utilized in Python:

Method 1: Convert Python String to Date Using “datetime.strptime()” Method

The “datetime.strptime()” method parses a string describing date and time into a DateTime object in Python.

Syntax

datetime.strptime(string, format)

 

In the above syntax:

  • The “string” parameter is the string representing the date and time to be parsed.
  • The “format” parameter determines how the date and time string will look.

Example

This code is utilized to convert the Python string into a date object:

from datetime import datetime
string_value = "Jan 13 2023 11:31PM"
print(string_value)
print(type(string_value), '\n')
datetime_obj = datetime.strptime(string_value, '%b %d %Y %I:%M%p')
print(datetime_obj)
print(type(datetime_obj))

 

In the above code:

  • The “datetime” from the “datetime” module is imported at the start.
  • The string datetime is initialized and displayed as an output.
  • The “strptime()” method takes the string object and the format string specifier. The format string specifier specifies how the string needs to be formatted.

Output

The DateTime object has been created accordingly by converting the input Python string.

Method 2: Convert Python String to Date Using “dateutil” Module

The standard datetime module gets additional features from the “dateutil” module. It includes a number of features for working with dates and times, such as parsing dates and times from strings in a variety of formats, etc. To convert a string to a datetime utilizing the “dateutil” module, the “parser.parse()” function is used in Python.

Example

The below code converts Python string to date using the discussed function:

from dateutil import parser
print(parser.parse("Feb 21 2023 09:21AM"))

 

In the above code lines, the “parser.parse()” function is used to parse the Python string into a datetime object.

Output

The specified Python string has been parsed into a Python datetime object.

Method 3: Convert Python String to Date Using “pandas.to_datetime()” Method

The “pandas.to_datetime()” method can also be utilized to convert a Python string to a datetime object. The “to_datetime()” method takes a string as its argument and parses the string according to a format string.

Example

The below example is used to convert the Python string to a DateTime:

import pandas
string_value = "Jan 13 2023 11:31PM"
print(string_value)
print(type(string_value), '\n')
obj = pandas.to_datetime(string_value)
print(obj)
print(type(obj))

 

In the above code snippet, the “pandas.to_datetime()” method takes the string as an argument and converts it into a datetime object.

Output

As analyzed, the defined string is converted into pandas date time object successfully.

Conclusion

The “datetime.strptime()” method, the “dateutil” module, or the “pandas.to_datetime()” method are used to convert a Python string to a datetime object. This guide presented various ways to create a Python datetime object from the given Python string.

Share Button

Source: linuxhint.com

Leave a Reply