 Python's datetime package provides a really convenient set of tools for working with dates and times. The rules for working with dates and times aren't overly complex, but they can be tricky to juggle gracefully all at once. For instance, several catastrophic bugs have come just from improperly handling time zones. While it's certainly feasible and occasionally desirable to handle dates and times explicitly in your code, the datetime package can be an incredible convenience. With just the five tricks that I'm about to show you, you can handle most of your datetime processing needs. Before jumping in though, it's helpful to look at how datetime is put together. The fundamental building block is a datetime object. Not surprisingly, this is a combination of a date object and a time object. The date object is just a set of values for the year, the month, and the day, and a collection of functions that knows how to handle them. A time object is structured in a similar way. It has values for the hour, the minute, the second, the microsecond, and the time zone. Any time can be represented by choosing these values appropriately. The first trick for working with datetimes is to be able to create them by combining date and time objects. To demonstrate this, we'll start by creating a time, passing it the hour of seven and the minute of zero. This represents seven o'clock. Because we didn't specify the second or the microsecond, these are assumed to be zero. Then we'll create a date by passing the year, and the month, and the day. Creating a datetime is straightforward. We use the combine function and pass it the date object and the time object that we want to build our datetime out of. Because of the naming convention, calls to datetime can be confusing. Datetime is the name of the package and a module within the package and the object that gets created. So when we combine our date and time, we call it with the apparently redundant datetime.datetime prefix. The first datetime, references the package. The second datetime, references the module. And combine is a function within that module. Our second trick for using datetimes is a type called time delta. This represents the difference between two datetimes. A time delta has just three values, days, seconds, and microseconds. It turns out that the difference between any two datetimes can be uniquely represented in this way. Time deltas are incredibly useful because they allow us to do simple addition and subtraction arithmetic on datetimes. They remove the need to think about things like how many days are in a month and how many seconds are in a day and leap years. Our third trick for getting the most out of datetimes is the use of time stamps. Working in days, hours, minutes, and seconds is awkward for computers. There are a lot of rules and corner cases to check and lots of branching logic. In order to make dates and times easier to work with, the notion of a Unix epic was created. This is the number of seconds that have passed since 12 a.m. January 1, 1970, in coordinated universal time. The UTC plus zero time zone. This allows any date and time to be represented by a single, commonly interpretable, floating point number. The only drawback is that it's not intuitive to a human reader. But the functions from timestamp and timestamp allow our human interpretable datetime object to be converted to and from a Unix epic for ease of computation. Trick number four in our bag is the weekday function. For any given date, it calculates the day of the week. To use it, call the date function on your datetime. This isolates the date object and ignores the time portion. And then call its weekday function. This returns a number from zero to six, where zero is Monday, one is Tuesday, etc., and six is Sunday. It handles all the quirks of keeping track of the days of the week, so you don't have to. Finally, we come to trick number five, which is converting a date to and from a string. This is particularly helpful when we're ingesting data from a text file and want to turn text dates into datetime objects. It's also helpful if we ever want to expose our datetime object to a user or export it to a text file. To do this, we'll use the strptime and strftime functions. When making a conversion in either direction, we have to supply a string that specifies the format. In this code snippet, %y represents the year, %m, the two-digit month, and %d, the two-digit day. As a side note, there is actually one correct way to represent years, months, and days in dates. An international standard, ISO 8601, established this in 1988. I highly recommend, whenever you're given the choice, to format your dates in this way for ease of interpretability and compatibility. However, be warned that there exists a multitude of date formats in the wild. Be prepared to perform some twisted conversions in order to get all of the data you ingest into that format. So now you're armed with the five most useful datetime tricks. Combine, time delta, converting to and from timestamps, weekday, and string formatting. With these in your toolbox, you're 90% of the way towards solving all of the datentime challenges in your next Python project. Good luck, and may it serve you well.