How to convert Python date to Unix timestamp?


A UNIX timestamp is the total number of seconds that have been counted since the epoch. An epoch is the starting point of time and is platform-dependent. The epoch is January 1, 1970, 00:00:00 (UTC) on Windows and most Unix systems, and leap seconds are not included in the time in seconds since the epoch.

In this article, we going to see how to convert a python date to a UNIX timestamp.

Datetime to UNIX timestamp

Here we convert a python date to a UNIX timestamp by using the time.mktime() method. In this example, we initially imported the datetime module. Then we have declared and assigned a datetime.time/date (2022, 6, 3, 12, 0, 50). time.mktime() is the inverse function of the local time. The time.mktime() method of the time module accepts a time.struct_time object or a tuple of 9 elements and returns a float value which represents the time since epoch in seconds.

Syntax

The syntax of the mktime() method is as follows.

time.mktime(t)

Where, t is time.struct_time object or a tuple containing 9 elements which responds to time.struct_time object.

The timetuple() method of datetime.date instance returns an object of type time.struct_time. This object is converted to a UNIX timestamp by using the time.mktime() method.

Example

Below is an example code to convert a python date to UNIX timestamp.

import datetime import time date_time = datetime.datetime(2022, 6, 3, 12, 0, 50) print("Given Date:",date_time) print("UNIX timestamp:", (time.mktime(date_time.timetuple())))

Output

The output of the above code is as follows;

Given Date: 2022-06-03 12:00:50
UNIX timestamp: 1654257650.0

Datetime to UNIX Timestamp in utc timezone.

Here we can use the datetime module to convert a datetime to a UTC timestamp in Python. If you already have the datetime object in UTC, you can the timestamp() to get a UTC timestamp. This function returns the time since epoch for that datetime object. If you have the datetime object in the local timezone, first replace the timezone info and then fetch the time.

Example

The following is an example code to convert a datetime to UNIX timestamp.

import datetime from datetime import timezone dt = datetime.datetime(2022, 6, 7) timestamp = dt.replace(tzinfo=timezone.utc).timestamp() print("The timestamp for the date 06-07-2022 is:",timestamp)

Output

The timestamp for the date 06-07-2022 is: 1654560000.0

Datetime string to UNIX Timestamp

In this case, we convert a datetime which is represented in a string format is converted to a UNIX Timestamp.

Example

import datetime given_date = "8/6/2022, 05:54:8" formated_date = datetime.datetime.strptime(given_date,"%m/%d/%Y, %H:%M:%S") Unix_timestamp = datetime.datetime.timestamp(formated_date) print("The Unix timestamp for the given input date is:") print(Unix_timestamp)

Output

The Unix timestamp for the given input date is:
1659765248.0

Updated on: 27-Aug-2023

25K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements