- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do I display the date, like "Aug 5th", using Python's strftime?
It is not possible to get a suffix like st, nd, rd and th using the strftime function. The strftime function doesn't have a directive that supports this formatting. You can create your own function to figure out the suffix and add it to the formatting string you provide.
from datetime import datetime now = datetime.now() def suffix(day): suffix = "" if 4 <= day <= 20 or 24 <= day <= 30: suffix = "th" else: suffix = ["st", "nd", "rd"][day % 10 - 1] return suffix my_date = now.strftime("%b %d" + suffix(now.day)) print(my_date)
This will give the output −
- How can I do "cd" in Python?
- What's the difference between "!!" and "?" in Kotlin?
- What's the difference between "STL" and "C++ Standard Library"?
- What's the difference between "update" and "update_idletasks" in Tkinter?
- How to return the "time" portion of the Date as a string using the current locale's conventions?
- C/C++ difference's between "int main()" and "int main(void)"
- What does the if __name__ == "__main__": do in Python?
- Short Date ("d") Format Specifier
- How do I redraw an image using Python's Matplotlib?
- How to print "Hello World!" using Python?
- What does the "===" operator do in Kotlin?
- What does "print >>" do in python?
- Sortable ("s") Format Specifier in C#
- Regular Expression "\S" Metacharacter in Java
- validation tefds11twcfwecfdweig11'"