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.
Headings of Contents
- 1 Convert Python Datetime Object To String
- 2 Python strftime()
- 3 How strftime() works
- 4 Convert Python Date to String
- 5 Convert Python Time to String
- 6 Convert Python Datetime to String
- 7 How to convert Timestamp to String in Python
- 8 Format Codes Table
- 9 Convert Datetime to locale’s appropriate format
- 10 Conclusion
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.
- 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
- Now we have used
now()
function to store the current date and time to thecurrent_date_time
variable.current_date_time = datetime.now() # current date and time
- 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 Code | Meaning | Example |
---|---|---|
%a | Weekday name | Sun, Mon |
%A | Full weekday name. | Sunday, Monday |
%w | Weekday as a decimal number. | 0,1,2,3,…6 |
%d | Day of the month as a zero-padded decimal. | 01,02,03,….31 |
%-d | Day of the month a decimal number. | 1,2,3,…31 |
%b | Month name. | Jun, Feb |
%B | Full month name. | January, February,… |
%m | Month as a zero-padded decimal. | 01, 02, ….12 |
%-m | Month as a decimal. | 1,2,3,….12 |
%y | A Year without century as a zero-padded decimal. | 00,01, 02, ..99 |
%-y | A year without century decimal number. | 0,1,2,..,99 |
%Y | A full year without century as a decimal number. | 2020, 2021, 2022 etc. |
%H | Hour ( 24-hour Clock ) as a zero-padded decimal number. | 00, 01, 02,…,23 |
%-H | Hour ( 24-hour Clock ) as a decimal number. | 0, 1, 2,…,23 |
%I | Hour ( 12-hour Clock ) as a zero-padded decimal number. | 01, 02,…,12 |
%-I | Hour ( 12-hour Clock ) as a decimal number. | 1, 2,…,12 |
%p | Local’s AM and PM | AM, PM |
%M | Minute as a zero-padded decimal number. | 00, 01, 02,…,59 |
%-M | Minute as a decimal number. | 0, 1, 2,…,59 |
%S | Second as a zero-padded decimal number. | 00, 01, 02,…,59 |
%-S | Second as a decimal number. | 0, 1, 2,…,59 |
%f | Microsecond as a zero-padded decimal number on the left. | 000000-999999 |
%c | Local appropriate date and time representation. | Sun Jan 9 13:50:29 2022 |
%x | Local appropriate date representation. | 01/09/22 |
%X | Local 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.