How to convert datetime string to datetime

How to convert datetime string to datetime

Python | Convert string to DateTime and vice-versa

Write a Python program to convert a given string to DateTime in Python.

Below are the methods that we will cover in this article:

Method 1: Program to convert string to DateTime using datetime.strptime() function.

strptime() is available in DateTime and time modules and is used for Date-Time Conversion. This function changes the given string of datetime into the desired format.

Examples:

Syntax of strptime function

The arguments date_string and format should be of string type.

Python3

Output:

Method 2: Program to convert DateTime to string using time.strftime

Python strftime() function is present in datetime and time modules to create a string representation based on the specified format string.

Examples:

Syntax of strftime function

Another similar function is available in time module which converts a tuple or struct_time object to a string as specified by the format argument.

Python3

Output:

Method 3: Program to convert DateTime to string using dateutil module

The dateutil is a third-party module. The parsing of dates in any string format is supported by the dateutil module. Internal facts about current world time zones are provided by this module. With the release of dateutil 2.0, it was recently adapted to Python 3, along with the parser functions. parse() can be used to convert a string into date-time format. The only parameter used is the string.

Источники информации:

    Добавить комментарий

    Ваш адрес email не будет опубликован. Обязательные поля помечены *