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"))
2. %A – It returns weekday in full version.
import datetime x = datetime.datetime.now() print (x.strftime("%A"))
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"))
4. %d – It returns day of month 01-31.
import datetime x = datetime.datetime.now() print (x.strftime("%d"))
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"))
6. %B – It returns month name in full version.
import datetime x = datetime.datetime.now() print (x.strftime("%B"))
7. %m – It returns month as a number 01-12.
import datetime x = datetime.datetime.now() print (x.strftime("%m"))
8. %y – It returns year in short version having without any century.
import datetime x = datetime.datetime.now() print (x.strftime("%y"))
9. %Y – It returns year in full version.
import datetime x = datetime.datetime.now() print (x.strftime("%Y"))
10. %H – It returns hour 00-23.
import datetime x = datetime.datetime.now() print (x.strftime("%H"))
11. %I – It returns hour 00-12.
import datetime x = datetime.datetime.now() print (x.strftime("%I"))
12. %p – It returns formate of time in AM/PM.
import datetime x = datetime.datetime.now() print (x.strftime("%p"))
13. %M – It returns minute 00-59.
import datetime x = datetime.datetime.now() print (x.strftime("%M"))
14. %S – It returns second 00-59.
import datetime x = datetime.datetime.now() print (x.strftime("%S"))
15. %f – It returns microsecond 000000-999999
import datetime x = datetime.datetime.now() print (x.strftime("%f"))
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"))
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"))
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"))
21. %c – It returns local version of date and time.
import datetime x = datetime.datetime.now() print (x.strftime("%c"))
22. %x – It returns local version of date.
import datetime x = datetime.datetime.now() print (x.strftime("%x"))
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"))
25. %% – It returns A % character.
import datetime x = datetime.datetime.now() print (x.strftime("%%"))
If you find anything incorrect in the above-discussed topic and have any further questions, please comment down below.
Like us on