In this video, Kathryn teaches you how to use the datetime module to get the current time and date into your code. Learn how to retrieve the current day, month, year, hour, minute, second, and timezone using the datetime module.
- [Narrator] We can use the datetime and calendar modules…to get information about previous, current,…and future dates and times.…We'll start off with the datetime module,…and so to import it, we're going to go from datetime,…and datetime here is the module,…and then we're going to go import datetime.…So here datetime is the module,…and we're importing the datetime class…from the datetime module.…Now that we have the datetime module here,…we can get information about the current time, date…as of right now when we run the program.…
So we're going to create a variable called "now".…And we're going to set it equal to datetime,…accessing that class, dot now.…And this will get the current date and time…according to this computer,…and it's going to be stored in the "now" variable.…To get the current dates,…all we have to do is go print now dot date.…And this is just a method.…We'll print it out, and there we go.…That's the current date.…We can also print out other things,…such as the year.…So we'll go now dot year.…
We'll print out the month with now dot month.…
- Working with logical and comparison operators
- Getting a list of numbers with the range() and list() functions
- Using mathematical functions such as round(), abs(), and pow()
- Calculating a given input's length
- Importing and using the math module
- Reading a user's command-line arguments
- Getting the current time
- Formatting dates and times with datetime
- Creating a timer
- Using urllib to get content from the Internet
- Using the JSON module to decode content
Skill Level Intermediate
1. Python: What's Built-In?
2. Python: Math
3. Python: Input/Output
4. Python: Date & Time, HTML Parsing, and HTTP
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.