

The above code should output “datetime2 is Greater” We can use an if statement to compare the two dates: if datetime1 > datetime2: In the second comparison, we are looking for the time delta showing us the time difference between the two objects.īefore we begin, we need a couple of datetime objects to work with: from datettime import datetimeĭatetime1 = datetime.strptime(' 02:45PM', '%m/%d/%Y %I:%M%p')ĭatetime2 = datetime.strptime(' 05:45PM', '%m/%d/%Y %I:%M%p') In the first comparison, we are looking for a Boolean True or False. Or you might want to know how much time has passed between two datetime objects. You may want to know whether one is greater than another, meaning if one came after the other. There are a few ways to compare datetime objects. Output: 1:29PM Comparing two datetime objects Mytime = datetime.strftime(datetime_object,'%m/%d/%Y’) Myyear = datetime.strftime(datetime_object,%Y’) If we wanted to just see one element, like the year, or the time, we could do that too: datetime_object = datetime.today() Notice we no longer have the time displayed, and we no longer have a string of zeroes stuck onto the end of it. The output should be something similar to “” Let’s pass this mask into the strftime (String Format Time) function along with our datetime_object variable and see what our output is: mydate = datetime.strftime(datetime_object,'%m/%d/%Y') If we want to display the above datetime as Monday/Day/Year, the mask would be “%m/%d/%Y”. To format this datetime, we need to use masks, just liked we used in the section for converting strings into datetime objects. If we print datetime_object, you should see something similar to this: 13:12:03.572480 The above code will populate the datetime_object variable with an object referencing the date and time right now.
#Convert string to date python how to
In this section, we will talk about how to take an existing datetime object, and display it in different ways.īefore we start, we need a datetime object to work with: from datetime import datetime We will address that in the section on formatting dates and times. However, it is not the same format as we passed in.

Notice we have the right date and time now. If we then print our datetime_object: print(datetime_object) This will eliminate that string of zeroes we saw before: datetime_object = datetime.strptime(' 02:45PM', '%m/%d/%Y %I:%M%p') If we happen to have a timestamp associated with our date, we can add that in as well. If we don’t want to use variables, we can hard code the date and/or the mask into the function call: datetime_object = datetime.strptime('', '%m/%d/%Y') If you don’t pass in a time value, then the value will always default to midnight. Datetime objects have a date and a time value. If we print the output of datetime_object, we will see that it shows: “ 00:00:00”Īll of the extra zeroes at the end are because we didn’t pass in a time. Next, let’s declare a variable and assign a string value to it: date1 = ""Īnd lets declare another variable containing our date mask: datemask = "%m/%d/%Y"įinally, let’s pass our date and mask into the strptime function: datetime_object = datetime.strptime(date1, datemask) We start by importing datetime: from datetime import datetime The strptime function takes two input variables: The main function you will use when converting a string is the strptime function. Converting from a stringīefore we can do anything else, we need to convert our string to a datetime object. A good resource for looking up additional maks variables can be found here. The above list is by no means comprehensive. Using the table above to construct our mask, we can describe our string of “” as “%m/%d/%Y” A few examples that we will use today are as follows: Description The datetime library has a special encoding you use to tell it which parts are which. Each part is separated by a forward slash. You can see that it starts with the month, followed by the day of the month, and ends with a 4 digit year.

Let’s say you have a date you want to convert “”. It helps the system understand the input better.

The mask references the format of the date you are trying to convert.
