To use a library you first need to import it with an import statement.
from datetime import date from datetime import time from datetime import datetime def main(): if __name__ == "__main__": main()
We are telling Python we want import the date, time and date time methods from the datetime module which is part of the Python core library.
Now that those classes are imported we can call the methods of those classes (for instance .today()
)
from datetime import date from datetime import time from datetime import datetime def main(): ## DATE OBJECTS # Get today's date from the simple today() method from the date class today = date.today() print "Today's date is ", today if __name__ == "__main__": main()
We can capture the result of calling the .today()
method on the date class in a variable today
for further use in the code.
We can look into the output from the today method in more depth:
from datetime import date from datetime import time from datetime import datetime def main(): ## DATE OBJECTS # Get today's date from the simple today() method from the date class today = date.today() print "Today's date is ", today # print out the date's individual components print "Date Components: ", today.day, today.month, today.year if __name__ == "__main__": main()
We can also work with properties like weekday number:
from datetime import date from datetime import time from datetime import datetime def main(): ## DATE OBJECTS # Get today's date from the simple today() method from the date class today = date.today() print "Today's date is ", today # print out the date's individual components print "Date Components: ", today.day, today.month, today.year # retrieve today's weekday (0=Monday, 6=Sunday) print "Today's Weekday #: ", today.weekday() if __name__ == "__main__": main()
Here are some properties from the datetime class and lastly one more from the date class:
from datetime import date from datetime import time from datetime import datetime def main(): ## DATE OBJECTS # Get today's date from the simple today() method from the date class today = date.today() print "Today's date is ", today # print out the date's individual components print "Date Components: ", today.day, today.month, today.year # retrieve today's weekday (0=Monday, 6=Sunday) print "Today's Weekday #: ", today.weekday() ## DATETIME OBJECTS # Get today's date from the datetime class today = datetime.now() print "The current date and time is ", today; # Get the current time t = datetime.time(datetime.now()) print "The current time is ", t if __name__ == "__main__": main()
Check out t = datetime.time(datetime.now())
, see how the time method of the date time class takes datetime.now()
as an argument. That makes sense...
The same for the date.weekday()
method, we feed it the argument today
which a variable set to the result of date time.now()
and Lastly...
from datetime import date from datetime import time from datetime import datetime def main(): # Get today's date from the datetime class today = datetime.now() # weekday returns 0 (monday) through 6 (sunday) wd = date.weekday(today) # Days start at 0 for Monday days = ["monday","tuesday","wednesday","thursday","friday","saturday","sunday"] print "Today is day number %d" % wd print "Which is a " + days[wd] if __name__ == "__main__": main()
We know that today = datetime.now()
returns the weekday number 0-6 and days is a list with 7 items, so each item has an index of 0-6.
We use that correspondance to reach into the days array and pull out the text for whatever index value datetime.now()
returns.
In this statement print "Today is day number %d" % wd
the %d
is a template placeholder and we immediately tell Python where is should get the value to replace that placeholder with % wd