Javatpoint Logo
Javatpoint Logo

Parse date from string python

Introduction: In this article, we are discussing parse date from string python. To convert a string to datetime in Python, use the strptime() method. strptime() is a built-in method of the datetime class used to convert a string representation of a date/time to a datetime object.

Syntax of parse date from string python: The syntax of the parsed date from string python is given below -

datetime- it is the class name.

strptime- it is the method name.

date_string- it is the string representation of the date.

Format- it is in the specified format of the date object.

Parameters of parsed date from string python: The parameter of the parsed date from string python is the strptime() function that accepts both required arguments and must be strings. The strptime() function is the exact opposite of the strftime() function, which converts datetime objects to strings.

Method: Write a Python application to convert a given string to DateTime in Python. Below are the methods that we can cover in this text.

  1. Use of datetime module
  2. Use of time module
  3. Use of dateutil module

1. Use of datetime module:For this, we use datetime.strptime() function. strptime() is available in the DateTime and Time modules and is used for datetime conversions. This function changes the given string from a datetime to the desired format. datetime. strptime() is a generic way to parse a string into a datetime. It can handle any format defined by a specified format string.

Example 1: Here, we give an example of a datetime module in python. The example is given below -

Output: Now we compile the above program, and after compilation, we run the above program. Then the result is given below -

2022-12-07 10:46:00

Example 2: Here, we give an example of a datetime module in python. The example is given below -

Output: Now we compile the above program, and after compilation, we run the above program. Then the result is given below -

2022-12-07

Example 3: Here, we give an example of a datetime module in python. The example is given below -

Output: Now we compile the above program, and after compilation, we run the above program. Then the result is given below -

<class 'datetime.datetime'>
2000-05-05 23:07:00

You can use strptime() to convert date and time in string format to datetime type. The first parameter is a string, and the second is a date and time format specifier. The advantage of converting to the date format is that you can independently select the month, date, or time. For more information about directives and strptime(), see Python strptime() - string to datetime object.

2. Use of time module: The Python function strftime() is found in the datetime and time modules and builds a string representation based on the given format string.

Example 1: Here, we give an example of a time module in python. The example is given below -

Output: Now we compile the above program, and after compilation, we run the above program. Then the result is given below -

May 05 2000 04:37:00 AM

3. Use of dateutil module: dateutil is a third-party module. The dateutil module supports parsing dates in arbitrary string formats. This module provides internal information about the current world time zone. Recently, dateutil 2.0 was released with parser functions and adapted for Python 3. You can use parse() to convert the string to datetime format. The only parameter used is a string.

Example 1: Here, we give an example of the dateutil module in python. The example is given below -

Output: Now we compile the above program, and after compilation, we run the above program. Then the result is given below -

2000-05-05 19:59:00
<class 'datetime.datetime'>






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA