Convert datetime to Integer in Python (3 Examples)

 

In this tutorial you’ll learn how to convert a datetime to an integer using the Python programming language.

The table of content is structured as follows:

Without further ado, let’s get into it!

 

Module Importing and Example Data Construction

Firstly we need to import the datetime module.

import datetime

Then we will set the current_time to a datetime variable.

current_date = datetime.datetime.now()

The current date is initially in this form:

print("String Format of Current Date and Time:", current_date)
# String Format of Current Date and Time: 2022-11-16 13:19:43.211656

 

Example 1: Utilizing the strftime Function

We can use the strftime() function to extract all the time fields, by the usage of the given parameters below.
%Y – year, %m – month, %d – day
%H – Hours, %M – Minutes, %S – Seconds

print("Date and Time in Integer Format:", int(current_date.strftime("%Y%m%d%H%M%S")))
# Date and Time in Integer Format: 20221116131943

We can change the formatting of the string as we wish, here is another way to express a date and time as an integer.

print("Date and Time in Integer Format:", int(current_date.strftime("%H%M%S%d%m%Y")))
# Date and Time in Integer Format: 13194316112022

 

Example 2: Stripping the Non-Numeric Characters

Another method is to convert the date into a string and then modify it based on this string. Initially, the datetime object also has microseconds, in this example we will simply get rid of that part.

First we need to convert the datetime object into a string in order to use the string functions on it. Then use the split() function to split the string into a group of substrings, which are separated by the character given as the parameter. The initial string is partitioned into two substrings with respect to the ‘.’ character and because we only want the first part of the list, we retrieve a substring at index 0. By doing so, we separate the microseconds part of the date.

curr_date_str = str(current_date)
curr_date_strip = curr_date_str.split('.')[0]

Afterwards we replace() the non-numeric characters with blanks, in other words we strip the date of non-numeric characters.

curr_date_strip = curr_date_strip.replace(':', '')
curr_date_strip = curr_date_strip.replace('-', '')
curr_date_strip = curr_date_strip.replace(' ', '')

These operations yield the following output:

print("Stripped Format of Current Date and Time:", int(curr_date_strip))
# Stripped Format of Current Date and Time: 20221112200439

 

Example 3: Utilizing Basic Math

Since each time unit has a certain number of digits:
Year – 4, Month – 2, Day – 2
Hours – 2, Minutes – 2, Seconds – 2

We can combine them to form an integer, in which the starting positions would be like this:
Year – 10, Month – 8, Day – 6
Hours – 4, Minutes – 2, Seconds – 0

Then we can utilize the starting points to multiply them with powers of 10 and add them up to express the date in an integer format.

print("Integer Format of Current Date and Time:",
      current_date.year * 10000000000 +
      current_date.month * 100000000 +
      current_date.day * 1000000 +
      current_date.hour * 10000 +
      current_date.minute * 100 +
      current_date.second)
# Integer Format of Current Date and Time: 202211122004 39

 

Video, Further Resources & Summary

Do you need more explanations on how to convert a date and time to an integer? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

 

The YouTube video will be added soon.

 

Furthermore, you could have a look at some of the other tutorials on Statistics Globe:

This post has shown how to convert a datetime to an integer. In case you have further questions, you may leave a comment below.

 

Ömer Ekiz Informatics Expert

This page was created in collaboration with Ömer Ekiz. You may have a look at Ömer’s author page to read more about his academic background and the other articles he has written for Statistics Globe.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top