Menu Close

How To Convert Datetime to String in Python

Convert Datetime to String in Python

In this Python guide, you will learn everything about How to convert DateTime to string in Python with various examples. Here we will use the Python DateTime module which provides the facility to convert Python DateTime object to string format.

Convert Python Datetime Object To String

Python has a built-in module name Datetime that contains a function strftime(). The strftime() function is used to Convert Datetime to String in Python. It takes a format as a parameter to convert DateTime to string.

Python DateTime module is a built-in module that means you don’t need to install it by using the Python pip command. It comes with Python by default.

Python strftime()

The strftime() function is defined inside the Python DateTime module so you need to import the DateTime module.

Example: Convert Datetime to String using strftime()


from datetime import datetime

current_date_time = datetime.now() # current date and time.

year = current_date_time.strftime("%Y")
print(f"Year is {year}")

month = current_date_time.strftime("%m")
print(f"Month is {month}")

day = current_date_time.strftime("%d")
print(f"Day is {day}")


time = current_date_time.strftime("%H:%M:%S")
print(f"Time is {time}")

When the above Python code is successfully executed the output will look like this.

Output


Year is 2022
Month is 01
Day is 09
Time is 12:38:21

Where year, day, month and time are string but now is the Python DateTime object. You can use the Python type() function to check the data type.

How strftime() works

Let’s see how the Python DateTime strftime() function works actually. The strftime() function takes format code as a parameter, This format may be one or multiple and convert DateTime to string according to format code. In the above example %Y, %m, %d, and %H:%M:%S are the format codes.

Here we will take the above example to explain this.

  1. First, we imported the DateTime class from DateTime module because the object of the DateTime class can be accessed strftime() function.
    from datetime import datetime
  2. Now we have used now() function to store the current date and time to the current_date_time variable.
    current_date_time = datetime.now() # current date and time
  3. The strftime() function is used to make formatted strings based on the format codes. Where %Y represents the year.
    current_date_time.strftime("%Y")

Convert Python Date to String

In this example, we are going to convert Python date to string only using strftime() function.

Example: Convert Python Date to String


from datetime import datetime

current_date_time = datetime.now() # current date and time.

# format only current date
date = current_date_time.strftime("%Y-%m-%d")
print(f"Date is {date}")

Output

Date is 2022-01-09

Convert Python Time to String

To convert the time to a string, we will use the strftime() function.

Example: Convert Python Time to String


from datetime import datetime

current_date_time = datetime.now() # current date and time.

# format only current time
time = current_date_time.strftime("%H:%M:%S")
print(f"Current time is {time}")

Output

Current time is 13:05:42

Convert Python Datetime to String

Here we are going to convert the DateTime object to a string using strftime() function.

Example: Convert Python Datetime to String


from datetime import datetime

current_date_time = datetime.now() # current date and time.

# format date time
date_time = current_date_time.strftime("%Y-%m-%d %H:%M:%S %p")
print(f"Current date and time is {date_time}")

Output

Current date and time is 2022-01-09 17:32:49 PM

How to convert Timestamp to String in Python

We have timestamp date and time, Now will convert in string date and time.


from datetime import datetime

timestamp = 1641716429

date_time = datetime.fromtimestamp(timestamp)

# format date time
date_time = date_time.strftime("%Y-%m-%d %H:%M:%S %p")
print(f"Current date and time is {date_time}")

Format Codes Table

You can use all these format code lists according to your requirements. These format codes are used to format the python DateTime object to a string.

Format CodeMeaningExample
%aWeekday nameSun, Mon
%AFull weekday name.Sunday, Monday
%wWeekday as a decimal number.0,1,2,3,…6
%dDay of the month as a zero-padded decimal.01,02,03,….31
%-dDay of the month a decimal number.1,2,3,…31
%bMonth name.Jun, Feb
%BFull month name.January, February,…
%mMonth as a zero-padded decimal.01, 02, ….12
%-mMonth as a decimal.1,2,3,….12
%yA Year without century as a zero-padded decimal.00,01, 02, ..99
%-yA year without century decimal number.0,1,2,..,99
%YA full year without century as a decimal number.2020, 2021, 2022 etc.
%HHour ( 24-hour Clock ) as a zero-padded decimal number.00, 01, 02,…,23
%-HHour ( 24-hour Clock ) as a decimal number.0, 1, 2,…,23
%IHour ( 12-hour Clock ) as a zero-padded decimal number.01, 02,…,12
%-IHour ( 12-hour Clock ) as a decimal number.1, 2,…,12
%pLocal’s AM and PMAM, PM
%MMinute as a zero-padded decimal number.00, 01, 02,…,59
%-MMinute as a decimal number.0, 1, 2,…,59
%SSecond as a zero-padded decimal number.00, 01, 02,…,59
%-SSecond as a decimal number.0, 1, 2,…,59
%fMicrosecond as a zero-padded decimal number on the left.000000-999999
%cLocal appropriate date and time representation.Sun Jan 9 13:50:29 2022
%xLocal appropriate date representation.01/09/22
%XLocal appropriate time representation.13:50:29

Convert Datetime to locale’s appropriate format

In this example, we will convert DateTime to the locale’s format using a single format code.

Example


from datetime import datetime

timestamp = 1641716429

date_time = datetime.fromtimestamp(timestamp)

# format date time
dt = date_time.strftime("%c")
print(f"Date and time:-  {dt}")

# format date
date = date_time.strftime("%x")
print(f"Date :-  {date}")

# format time
time = date_time.strftime("%X")
print(f"Time:-  {time}")

Output


Date and time:-  Sun Jan  9 13:50:29 2022
Date :-  01/09/22
Time:-  13:50:29

Conclusion

So, in this article, we have seen all about how to convert DateTime to string in Python with the help of various examples. This will be very helpful when you are working on a real-life project or going to be working on any real-life project because in real life project maximum we need to convert DateTime object to string or timestamp date time to string.

You can use any formatting code to format your DateTime object using strftime() function. I hope you have understood this article very well. If you like this article, please share and keep visiting for further Python tutorials.

Previous Article

Related Posts