How to Format Date Time Data Object in Python

In this tutorial, I will show how to format date time data object in Python. 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. 01/29/12

01/29/2012

29.01.2012

19. %X It is the representation of the preferred time without a date. 21:45:56
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, +0600, +1030

23. %% A literal ‘%’ character. %

I have another article How to Convert Date and Time to String in Python, from where you can learn about different usability of strftime() function.

In this tutorial, I tried to brief commonly used Python strftime() format directives. Hope you have enjoyed the tutorial. If you want to get updated, like my facebook page https://www.facebook.com/LearningBigDataAnalytics and stay connected.

Add a Comment