Working with DateTime objects in Python

When you read a date or time from a text file, user input, or a database, you are likely to get the date information as a string. It is helpful to convert the string to a datetime object since it will allow you to do more advanced functions. In todays article I will discuss and show examples of datetime objects in python. Specifically, I will show how to convert a string to a datetime, how to compare and reformat datetime variables, how to work with timezones, and how to extract specific bits of information.

What are Masks?

The mask references the format of the date you are trying to convert. It helps the system understand the input better. Let’s say you have a date you want to convert “7/11/2019”. You can see that it starts with the month, followed by the day of the month, and ends with a 4 digit year. Each part is separated by a forward slash. The datetime library has a speacial encoding you use to tell it which parts are which. A few examples that we will use today are as follows:

DescriptionVaraible
Year%Y
Month%m
Day of Month%d
Hour of Day (12 hour clock)%I
Minute of Hour%M
AM/PM%p
Hour of day (24 hour clock)H

Using the table above to construct our mask, we can describe our string of: “7/11/2019” as “%m/%d/%Y”

The above list is by no means comprehensive. A good resource for looking up addutional maks variables can be found here.

Converting from a string

Before we can do anything else, we need to convert our string to a datetime object. The main function you will use when converting a string is the strptime function. This stands for String Parse Time. The strptime funciton takes two input variables:

  • The date/time you wish to parse
  • The mask of what you are parsing.

Calling the strptime function

We start by importing datetime:

Next let’s declare a variable and assign a string value to it:

And lets declare another variable containing our date mask:

Finally lets pass our date and mask into the strptime function:

if we print the output of datetime_object, we will see that it shows:”2019-07-11 00:00:00″

All of the extra zeroes at the end are because we didn’t pass in a time. Datetime objects have a date and a time value. If you don’t pass in a time value, then the value will always default to midnight.

If we don’t want to use varibles, we can hardcode the date and/or the mask into the function call:

If we happen to have a timestamp associated with our date, we can add that in as well. This will eliminate that string of zeroes we saw before:

If we then print our datetime_object:

The output should be:2019-07-11 14:45:00

Notice we have the right date and time now. However, it is not the same format as we passed in. We will address that in the section on formatting dates and times.

Formatting Date and Time

In this section we will talk about how to take an existing datetime object, and display it in different ways.

Before we start, we need a datetime object to work with:

The above code will populate the datetime_object varaible with an object referencing the date and time right now. If we print datetime_object, you should see something similar to this:2018-03-11 13:12:03.572480

To format this datetime, we need to use masks, just liked we used in the section for converting strings into datetime objects. If we want to display the above datetime as Monday/Day/Year, the mask would be “%m/%d/%Y”.

Let’s pass this mask into the strftime (String Format Time) function along with our datetime_object variable and see what our output is:

The output should be something similar to:”07/11/2019″

Notice we no longer have the time displayed, and we no longer have a string of zeroes stuck onto the end of it.

If we wanted to just see one element, like the year, or the time, we could do that too:

Output: 2018

Output: 1:29PM

Comparing two datetime objects

There are a few ways to compare datetime objects. You may want to know whether one is greater than another, meaning if one came after the other. Or you might want to know how much time has passed between two datetime objects.

In the first comparison, we are looking for a Boolean True or False. In the second comparison, we are looking for the time delta showing us the time difference between the two objects.

Before we begin, we need a couple of datetime objects to work with:

We can use an if statement ot compare the two dates:

The above code should output “datetime2 is Greater”

Now that we know that datetime2 is greater, meaning it came after datetime1. It would be nice to know how much later datetime2 is than datetime1.

Lucky for us, it is just like doing some simple math:

The output should be similar to: 31 days, 3:00:00

This indicates there is a 31 day and 3 hour difference between the two timestamps. What if we just want to get a number back indicating a given unit of time? The variable type returned when we subtract one time from another is timedelta. The timedelta object has attributes for each unit of time. If we want to know how many days have passed, we could run:

We will see 31 printed on the screen.

If we want to know how many seconds, we would run:

The output would be:10800

If we take 10,800 and divide it by 60 we get 180, then we divide it by 60 again, we get 60. You will notice that this is just showing us the second part of the 31 days and 3 hours.

You will also notice if you try to print timediff.hours or timediff.weeks that it does not work. The only three attributes you can print in this fashion are:

  • days
  • seconds
  • milliseconds

If you want to know how many total hours have passed between datetime1 and datetime2, you will need to do a little math. There is another function we have not used yet called total_seconds(). This function shows you the total number of seconds from the start time to the end time:

Output should be: 747

This indicates taht 747 hours have passed between timedate1 and timedate2

Converting timezones

Thre are a lot of timezones in the world, and it can sometimes be confusing to determine which event happened first. One way to handle this is to always use UTC time (Coordinated Universal Time). But you don’t always have control over that.

There is one prerequisite for this section, and that is pytz. to install that, run:

– or –

In our example, lets say you have two airplanes. One is landing in San Francisco (SFO), the other is landing at New York (JFK). One lands at 3:00 PM, San Francisco Time. The other plane lands at 5:00 PM New York time. Which one landed first?

First we import our libraries

Next we declare a few of variables to represent our timezones:

Then we create our two datetime variables:

If we print the times, they should look like this:

Output:
2019-07-11 15:00:00
2019-07-11 17:00:00

We can now use the localize function from pytz to assign a timezone to our respective variables:

We can verify that it worked by printing out the tzinfo and times again:

Output:2019-07-11 15:00:00-07:00US/Pacific2019-07-11 17:00:00-04:00US/Eastern

Notice in the output, it now shows us the name of the timezone when we print the .tzinfo attribute. It also shows us the UTC offset when we print the time. Notice the -07 and -04 for the Pacific and Eastern times.

Now if we compare the two times:

The above code will print “JFK was first”. This works because the system now knows about the timezones.

For a list of all timezones in the pytz module, you can run the following code:

 

Summary

We have discussed how to convert strings into datetime objects, how to compare two different datetime objects, and how to introduce timezone attributes to your datetime attributes.

%d bloggers like this: