python-strftime-method

Python strftime method of Date-Time module with Practical Examples

In This Article You Will Learn About Python strftime method.

Python strftime method – Before moving ahead, let’s know a little bit about Python Date-Time

The strftime() Method – The datetime object has a method strftime() to format into strings. It takes one parameter – format, to specify the format of the returned string.

1. %a – It returns weekday in short version.

import datetime
x = datetime.datetime.now()

print (x.strftime("%a"))
python-strftime-method

2. %A – It returns weekday in full version.

import datetime
x = datetime.datetime.now()

print (x.strftime("%A"))
python-strftime-method

3. %w – It returns weekday as a number 0-6. By default it returns, 0 as Sunday

import datetime
x = datetime.datetime.now()

print (x.strftime("%w"))
python-strftime-method

4. %d – It returns day of month 01-31.

import datetime
x = datetime.datetime.now()

print (x.strftime("%d"))
python-strftime-method

5. %b – It returns month name in short version (first letter is in upper case rest of in lower case).

import datetime
x = datetime.datetime.now()

print (x.strftime("%b"))
python-strftime-method

6. %B – It returns month name in full version.

import datetime
x = datetime.datetime.now()

print (x.strftime("%B"))
python-strftime-method

7. %m – It returns month as a number 01-12.

import datetime
x = datetime.datetime.now()

print (x.strftime("%m"))
python-strftime-method

8. %y – It returns year in short version having without any century.

import datetime
x = datetime.datetime.now()

print (x.strftime("%y"))
python-strftime-method

9. %Y – It returns year in full version.

import datetime
x = datetime.datetime.now()

print (x.strftime("%Y"))
python-strftime-method

10. %H – It returns hour 00-23.

import datetime
x = datetime.datetime.now()

print (x.strftime("%H"))
python-strftime-method

11. %I – It returns hour 00-12.

import datetime
x = datetime.datetime.now()

print (x.strftime("%I"))
python-strftime-method

12. %p – It returns formate of time in AM/PM.

import datetime
x = datetime.datetime.now()

print (x.strftime("%p"))
python-strftime-method

13. %M – It returns minute 00-59.

import datetime
x = datetime.datetime.now()

print (x.strftime("%M"))
python-strftime-method

14. %S – It returns second 00-59.

import datetime
x = datetime.datetime.now()

print (x.strftime("%S"))
python-strftime-method

15. %f – It returns microsecond 000000-999999

import datetime
x = datetime.datetime.now()

print (x.strftime("%f"))
python-strftime-method

16. %z – It returns UTC offset.

import datetime
x = datetime.datetime.now()


print (x.strftime("%z"))

Explanation – In the above example, code shows UTC offset.

17. %Z – It returns timezone.

import datetime
x = datetime.datetime.now()

print (x.strftime("%Z"))

Explanation – In the above example, code shows Time Zone.

18. %j – It returns day number of year 001-366.

import datetime
x = datetime.datetime.now()

print (x.strftime("%j"))
python-strftime-method

19. %U – It returns week number of year, Sunday as the first day of week, 00-53.

import datetime
x = datetime.datetime.now()

print (x.strftime("%U"))
python-strftime-method

20. %W – It returns week number of year, Monday as the first day of week, 00-53.

import datetime
x = datetime.datetime.now(2020, 6, 20)

print (x.strftime("%W"))
python-strftime-method

21. %c – It returns local version of date and time.

import datetime
x = datetime.datetime.now()

print (x.strftime("%c"))
python-strftime-method

22. %x – It returns local version of date.

import datetime
x = datetime.datetime.now()

print (x.strftime("%x"))
python-strftime-method

23. %X – It returns local version of time.

import datetime
x = datetime.datetime.now()

print (x.strftime("%X"))

24. %V – It returns week number (01-53).

import datetime
x = datetime.datetime.now()                                                                   

print (x.strftime("%V"))
python-strftime-method

25. %% – It returns A % character.

import datetime
x = datetime.datetime.now()

print (x.strftime("%%"))
python-strftime-method

If you find anything incorrect in the above-discussed topic and have any further questions, please comment down below.

Like us on

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on