Using datetime Methods and Attributes
00:00
There are several attributes in the datetime
module that you can call on a datetime
object. If you want to see the current month, you can print your object followed by the .month
attribute, which is .month
.
00:11
The current month is October, so when you call now.month
, it returns 10
, as October is the tenth month of the year. As another example, if you wanted to see just the current year, you could use the print()
function on your datetime
object now
, followed by the .year
attribute, which is .year
.
00:28
As you can see, it returns the current year, which is 2023
.
00:32
There are several other attributes and methods that the datetime
module allows you to call for a comprehensive list of all of these attributes and methods.
00:39
You can use your datetime
object as a parameter in the dir()
function to return all of the attributes and methods of your datetime
object.
00:47
As you can see, there are many attributes and methods associated with datetime
objects as shown when calling the dir()
function.
00:54
For example, you can call the .second
attribute to show the current second in the current minute out of sixty, or you can call the .weekday()
method to return the day of the week as an integer, with Monday being zero and Sunday being six.
01:08
If you want to learn more about what each of them do, you can test them out in your REPL or refer to the datetime
documentation.
Become a Member to join the conversation.