VOOZH about

URL: https://www.geeksforgeeks.org/python/convert-date-string-to-timestamp-in-python/

⇱ Convert Date String to Timestamp in Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Convert Date String to Timestamp in Python

Last Updated : 29 Oct, 2025

Given a date in string format, the task is to convert it into a Unix timestamp using Python. A timestamp represents the total number of seconds that have passed since January 1, 1970 (the Unix Epoch).

For example:

Input: "20/01/2020"
Output: 1579478400.0

Let’s explore different methods to perform this conversion.

Using datetime.timestamp()

datetime.timestamp() converts a datetime object to a Unix timestamp in seconds and returns a float. It assumes local time if no timezone is set.


Output
1579478400.0

Explanation: s is date string, which is parsed into a datetime object dt using the format %d/%m/%Y. dt.timestamp() then converts it to a Unix timestamp, which is stored in res.

Using calendar.timegm()

calendar.timegm() method takes a UTC time tuple and returns an integer timestamp, making it ideal when you need timezone-independent, UTC-based results.


Output
1579478400

Explanation: s is date string, parsed into a datetime object dt.calendar.timegm(dt.utctimetuple()) converts it to a UTC-based Unix timestamp, stored in res.

Using pandas.to_datetime()

pandas.to_datetime().timestamp() parses a date string into a datetime object and returns a float timestamp. It is perfect for handling large datasets or performing time-based data analysis using pandas.


Output
1579478400.0

Explanation: s is date string, parsed by pd.to_datetime() and converted to a Unix timestamp using .timestamp(), stored in res.

Using time.mktime()

time.mktime() converts a local time tuple to a timestamp and returns a float; it reflects your system’s timezone, so it’s best for local time operations.


Output
1579478400.0

Explanation: s holds the date string, parsed into dt. time.mktime(dt.timetuple()) converts it to a local Unix timestamp, stored in res.

Related articles

Comment