- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
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(). 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.
The syntax of the mktime() method is as follows.
Where, t is time.struct_time object or a tuple containg 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.
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())))
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.
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)
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.
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)
The Unix timestamp for the given input date is: 1659765248.0
- Related Articles
- How to convert unix timestamp string to readable date in Python?
- Convert MySQL Unix-Timestamp format to date format?
- Convert MySQL timestamp to UNIX Timestamp?
- How to convert from Unix timestamp to MySQL timestamp value?
- How to convert MySQL datetime to Unix timestamp?
- Convert date string to timestamp in Python
- MySQL - Convert YYYY-MM-DD to UNIX timestamp
- How to convert date to timestamp in MongoDB
- Convert MM/DD/YY to Unix timestamp in MySQL?
- Convert dd/mm/yyyy string to Unix timestamp in MySQL?
- How to get the Unix timestamp in C#
- Convert DATE timestamp to return the month number
- Convert UNIX timestamp into human readable format in MySQL?
- How to convert a timestamp object in to Date in JDBC program?