Formatting dates and times in Python with datetime

Keyboard Shortcuts

In this video, Kathryn shows you how to use the datetime module to format the current time retrieved in the previous video. She demonstrates using the strftime function to make the datetime look more recognizable to the user.

Now, everything we've written so far has only come in…one format, but we can actually control the format of our…dates and times with strings.…We'll use something called "shift time"…with he shift time method.…It takes one string, and what's in the string determines…how the contents of the "now" variable are displayed.…First, we'll control how the day of a given week or month…is displayed.…With %a we can have an abbreviated day of the week…with just Mon, Tues, Wed displayed.…With %A, we can show the full name of the day of the week.…

So Monday, Tuesday, et cetera.…And we can also display the day as the day of the month…with %d.…So if it was going to be the tenth day of the month,…%d would translate to ten.…Trying this out we'll go…now dot S-T-R-F-T-I-M-E,…%a, %A and then %d.…So this will display the abbreviated day of the week,…the full day of the week, and then the numbered day…of the week.…

Printing this out we'll add a print statement here.…Run the code.…And we get Wed Wednesday and it's the fourteenth…of the month.…

Resume Transcript Auto-Scroll

Author

Released

2/15/2017

Python libraries can cut your development time and reduce your frustration with coding. In this Python 3 course, follow Kathryn Hodge as she walks through the Python standard library. Discover how familiarizing yourself with its facilities can make writing code in this concise, elegant language a little easier. Familiarize yourself with the library's built-in functions, math modules, and input and output modules. Learn how to get information from the Internet with the HTTP module, and more.