Working With Dates and Times
Introduction to Python
In this first course, we introduce the Python Language, the declaration model, and how variables and functions are used in python.
Our learning objectives for this course are to introduce the python language and to be able to recognize and explain the core concepts of the Python language.
Hi, and welcome back! Let's look at dates and times. So our learning objectives for this lecture are to learn how to create date and time objects, making date and time calculations, getting Unix-style date stamps, formatting dates, parsing and working with calendars. Python provides several modules for working with dates and times. The standard library includes the datetime module, which provides the date, datetime, and time classes. These basic classes cover the most datetime-related needs. The time module in the standard library works with times, including dates, in either e-POP time, or time tuple format. The calender module has tools for generating calendars in text and HTML. If you need to parse dates from text or work with the time zones, the dateutil module, which is not in the standard library, but which is included with Anaconda, is very useful. The arrow module attempts to consolidate all datetime-related functionality into a single, easy-to-use module. Let's look at ways to store dates and times. There are three ways, at least, in which dates and times can be stored. The classes from the datetime module store time data as date, time, or datetime objects. Routines in the time module return a given datetime as a Unix e-POP time, which is the number of seconds since December 31st, 1969. For some functions return a nine-element tuple of year, month, day, hour, minute, second, weekday, days, since December 31st. The dateutil module uses the classes from datetime, as does arrow.
Python provides the datetime module for manipulating dates and times. Once you have created date or time objects, you can combine them and extract the time units you need. The date object contains a valid year, month and day. The time object contains a valid hour, minute, second, and microsecond. The datetime object combines a date with a time. Timedelta presents the difference between the two preceding dates. If we look through our example code here, the first line gets today's date, then we get today's date and time, then a datetime object that has attributes for datetime paths. Number four, it creates a date object; line five, date objects can be subtracted from other date objects; and six shows shows the timedelta that has days, seconds, and microseconds. Seven lets us create a timedelta of 10 days, and number eight allows us to add timedelta to datetime. And here we create a datetime object. So how do we go about formatting dates and times? In our example script here, our first line creates the datetime object; and then second, we print using the default format; and third, we format giving us a output print. The range on three is really zero to 61, and this accounts for leap seconds and the very rare double leap second. If utcoffset returns timedelta and minutes=-30, %z is replaced with the string, -0330.
Andrew is fanatical about helping business teams gain the maximum ROI possible from adopting, using, and optimizing Public Cloud Services. Having built 70+ Cloud Academy courses, Andrew has helped over 50,000 students master cloud computing by sharing the skills and experiences he gained during 20+ years leading digital teams in code and consulting. Before joining Cloud Academy, Andrew worked for AWS and for AWS technology partners Ooyala and Adobe.