In Python, date and time are not a data type of its own, but a module named
datetime can be imported to work with the date as well as time. Datetime module comes built into Python, so there is no need to install it externally. The datetime module in Python provides classes with numerous functions to deal with date and time.
The strftime() function is used to convert date and time objects to their string representation. It takes one or more input of formatted code and returns the string representation.
Returns : It returns the string representation of the date or time object.
List of format codes : Reference table for the format codes.
|%a||Abbreviated weekday name.||Sun, Mon, …|
|%A||Full weekday name.||Sunday, Monday, …|
|%w||Weekday as a decimal number.||0, 1, …, 6|
|%d||Day of the month as a zero added decimal.||01, 02, …, 31|
|%-d||Day of the month as a decimal number.||1, 2, …, 30|
|%b||Abbreviated month name.||Jan, Feb, …, Dec|
|%B||Full month name.||January, February, …|
|%m||Month as a zero added decimal number.||01, 02, …, 12|
|%-m||Month as a decimal number.||1, 2, …, 12|
|%y||Year without century as a zero added decimal number.||00, 01, …, 99|
|%-y||Year without century as a decimal number.||0, 1, …, 99|
|%Y||Year with century as a decimal number.||2013, 2019 etc.|
|%H||Hour (24-hour clock) as a zero added decimal number.||00, 01, …, 23|
|%-H||Hour (24-hour clock) as a decimal number.||0, 1, …, 23|
|%I||Hour (12-hour clock) as a zero added decimal number.||01, 02, …, 12|
|%-I||Hour (12-hour clock) as a decimal number.||1, 2, … 12|
|%p||Locale’s AM or PM.||AM, PM|
|%M||Minute as a zero added decimal number.||00, 01, …, 59|
|%-M||Minute as a decimal number.||0, 1, …, 59|
|%S||Second as a zero added decimal number.||00, 01, …, 59|
|%-S||Second as a decimal number.||0, 1, …, 59|
|%f||Microsecond as a decimal number, zero added on the left.||000000 – 999999|
|%z||UTC offset in the form +HHMM or -HHMM.|
|%Z||Time zone name.|
|%j||Day of the year as a zero added decimal number.||001, 002, …, 366|
|%-j||Day of the year as a decimal number.||1, 2, …, 366|
|%U||Week number of the year (Sunday as the first day of the week). All days in a new year preceding the first Sunday are considered to be in week 0.||00, 01, …, 53|
|%W||Week number of the year (Monday as the first day of the week). All days in a new year preceding the first Monday are considered to be in week 0.||00, 01, …, 53|
Without formatting 2019-12-17 18:21:39.211378 Example 1: Tue-12-19 Example 2: Tuesday-12-2019 Example 3: 6 PM 39 Example 4: 351
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- time.strftime() function in Python
- Python | Pandas DatetimeIndex.strftime()
- Python | Pandas Period.strftime
- Python | Pandas PeriodIndex.strftime
- Python | Pandas Series.dt.strftime
- Python - Call function from another function
- Wand function() function in Python
- Returning a function from a function - Python
- wxPython - GetField() function function in wx.StatusBar
- How to write an empty function in Python - pass statement?
- Function Decorators in Python | Set 1 (Introduction)
- Vulnerability in input() function – Python 2.x
- Function Annotations in Python
- Sorted() function in Python
- Ways to sort list of dictionaries by values in Python - Using lambda function
- Python Numbers | choice() function
- Python | askopenfile() function in Tkinter
- Python | Binding function in Tkinter
- floor() and ceil() function Python
- ord() function in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.