Signup/Sign In
PUBLISHED ON: AUGUST 26, 2021

How to convert date object to string in Python

In this tutorial, we will learn how to convert a given date object to a string using the strftime() function.

Strftime() function

In python, there is a function called strftime() which converts a datetime, time, and date object into a string object according to the given format.

This strftime() function is available in datetime and time modules.

Syntax:

datetime.strftime(format)

The following table contains the most commonly used Python strftime() format directives.

Sl.no

Directive

Meaning Example
1. %a

It is Weekday’s abbreviated name

Sun, Mon, Tue, Wed, Thu, Fri, Sat
2. %A

It is Weekday’s full name.

Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
3.

%b

It is an abbreviated month’s name. Jan, Feb, …, Dec
4. %B

It is the full name of Months.

January, February, …, December
5. %c

It is the representation of the preferred date and time.

Sat Jan 09 21:30:00 2021
6. %d

Day of the month as a zero-padded decimal number. (01 to 31)

01, 02, …, 31
7. %f It is a decimal number representing the Microsecond.

000000, 000001, …, 999999

Not applicable to the time module.

8. %H

The decimal number represents the Hour, 24-hour clock format(00 to 23)

01, 02, … , 23
9. %I

The decimal number represents the Hour, 12-hour clock format(01 to 12)

01, 02, … , 12
10. %j

It is a decimal number that represents the Day of the year.

001, 002, …, 366
11. %m

Month as a zero-padded decimal number(01 to 12).

01, 02 … 12
12. %M

It is a decimal number that represents the Minute (01 to 59).

01, 02, … , 59
13. %p

It represents either AM or PM corresponding to the given time value.

AM, PM
14. %S

It is a decimal number that represents the Second (01 to 59).

01, 02, … , 59
15. %U

Week number of the current year

Sunday is the first day of the first week.

00, 01, …, 53
16. %w

The decimal number represents the day of the week.

0, 1, 2, 3, 4, 5, 6
17. %W

Week number of the current year

Monday is the first day of the first week.

00, 01, …, 53
18. %x

It is the representation of preferred dates without time.

08/16/88

08/16/1988

16.08.1988

19. %X

It is the representation of the preferred time without a date.

21:30:00
20. %y

The decimal number represents the year without a century (from 00 to 99).

01, 02, … 99
21. %Y

The decimal number represents the year with the century.

0001, 0002, … , 9999
22. %Z or %z Time zone name.

(empty), UTC, IST, CST

+0000, -0400, +1030

23. %%

A literal ‘%’ character.

%

Consider examples to understand how the strftime() method works.

How to get the current time and date

In python, we can get the current date and time using the datetime object.

In the datetime module, the now() function gives us the current local date and time.

Example 1:

The below example shows how to get the current time and date using the now() function.

import datetime
current_datetime=datetime.datetime.now()
print("current date and time:",current_datetime)


current date and time: 2021-01-10 14:54:36.638514

Example: Convert current date and time to string

By using the strftime() function, we can format the current date and time.

We already know the formats directives that the strftime() function uses from the table.

import datetime
current_datetime=datetime.datetime.now()
print("current date and time:",current_datetime)
str_date = current_datetime.strftime("%d-%m-%Y")
print("current date and time:",str_date)
print(type(str_date))
print("The Current day is: ",current_datetime.strftime("%d"))
print("The Current week is: ",current_datetime.strftime("%W"))
print("The Current month is: ",current_datetime.strftime("%B"))
print(" TheCurrent year is: ",current_datetime.strftime("%Y"))
print("Current Hour in a 24-hour clock format: ",current_datetime.strftime("%H"))
print("AM OR PM : ",current_datetime.strftime("%p"))


current date and time: 2021-01-10 15:35:08.879407
The current day is: 10
The current week is: 01
The current month is: January
The current year is: 2021
Current Hour in a 24-hour clock format: 15
AM OR PM: PM

Now, we know how to use the strftime() function. So in the next example try to convert date objects to strings in different formats.

Example 3:

The below example shows how to convert a date object to a string.

import datetime
current_datetime=datetime.datetime.now()
print("current date and time:",current_datetime)
print("Current day is: ",current_datetime.strftime("%d"))
print("Current week is: ",current_datetime.strftime("%W"))
print("Current month is: ",current_datetime.strftime("%B"))
print("Current year is: ",current_datetime.strftime("%Y"))
print("Current Hour in 24-hour clock format: ",current_datetime.strftime("%H"))
print("AM OR PM : ",current_datetime.strftime("%p"))


<class 'datetime.datetime'>
Date in different formats:
Friday, August 1947
Fri Aug 47
Aug/Friday/1947
Aug-Fri-1947
Aug,15,1947
Aug,15,1947
<class 'str'>

In the above code, we converted the date object into different string formats.



About the author:
An enthusiastic fresher, a patient person who loves to work in diverse fields. I am a creative person and always present the work with utmost perfection.