Converting a String to DateTime in Python:
In this article, we will discuss the various ways of converting a string to a datetime object in Python, and we will write programs to demonstrate the same.
Method 1: Using datetime library
The datetime library in Python provides a datetime class, which is used to represent dates and times. We can use the datetime.strptime()
method to convert a string to a datetime object. The strptime()
method takes two arguments: the first argument is the string that we want to convert, and the second argument is the format string that specifies the format of the string.
Example:
import datetime
date_str = '2022-03-27 12:30:45'
date_obj = datetime.datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S')
print("Date object:", date_obj)
print("Type of date object:", type(date_obj))
Output:
Date object: 2022-03-27 12:30:45
Type of date object: <class 'datetime.datetime'>
In the above program, we first import the datetime module. Then, we define a date_string variable that contains a date and time string in the format of "YYYY-MM-DD HH:MM:SS
". We then use the datetime.datetime.strptime()
method to convert the string to a datetime object. The format string '%Y-%m-%d %H:%M:%S
' specifies that the year is represented by four digits (Y), the month is represented by two digits (m), the day is represented by two digits (d), the hour is represented by two digits (H), the minute is represented by two digits (M), and the second is represented by two digits (S).
We then print the date object and its type using the print()
function.
Method 2: Using time library
The time library in Python provides a strptime()
method that can be used to convert a string to a struct_time object, which can be further converted to a datetime object using the datetime.datetime()
method.
Here is an example program to convert a string to a datetime object using the time library:
import time
import datetime
date_str = '2022-03-27 12:30:45'
time_str = time.strptime(date_str, '%Y-%m-%d %H:%M:%S')
date_obj = datetime.datetime.fromtimestamp(time.mktime(time_str))
print("Date object:", date_obj)
print("Type of date object:", type(date_obj))
Output:
Date object: 2022-03-27 12:30:45
Type of date object: <class 'datetime.datetime'>
In the above program, we first import the time and datetime modules. Then, we define a date_string variable that contains a date and time string in the format of "YYYY-MM-DD HH:MM:SS
". We then use the time.strptime()
method to convert the string to a struct_time object. The format string '%Y-%m-%d %H:%M:%S
' specifies the same format as in the previous example.
We then use the datetime.datetime.fromtimestamp()
method to convert the struct_time object to a datetime object. The method takes the output of the time.mktime() method, which converts the struct_time
object to a timestamp.
Method 3: Using dateutil library
The dateutil library in Python provides a parser class that can be used to parse date and time strings in different formats.
Here is an example program to convert a string to a datetime object using the dateutil library:
from dateutil import parser
date_str = '2022-03-27 12:30:45'
date_obj = parser.parse(date_str)
print("Date object:", date_obj)
print("Type of date object:", type(date_obj))
Output:
Date object: 2022-03-27 12:30:45
Type of date object: <class 'datetime.datetime'>
In the above program, we first import the parser class from the dateutil
module. Then, we define a date_string variable that contains a date and time string in the format of "YYYY-MM-DD HH:MM:SS
". We then use the parser.parse() method to convert the string to a datetime object.
Comments (0)