TNS
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
NEW! Try Stackie AI
Python

Python: Introduction to Timestamps and Time Strings

Python has the ability convert dates and time into machine-readable timestamps, and back again. Find out how, here.
Jul 27th, 2025 7:05am by
Featued image for: Python: Introduction to Timestamps and Time Strings
Feature image via Unsplash.

A timestamp can be thought of as a numerical record that captures exactly when something happened. These timestamps are stored as cumulative seconds since the Unix epoch of January 1st, 1970. Everything after that date is counted in terms of how many seconds have passed from the starting point. For example, July 23, 2025, would be 1753280905 — because that’s how many seconds have passed since the epoch.

A time string, on the other hand, is a collection of characters that represent a moment in time, such as “July 23, 2025.”

These two things are very different from one another.

Converting Timestamp to Time String Using Python

Most people would have trouble knowing or calculating a timestamp. Because of that, it would be very challenging to use a timestamp in your Python scripts or applications. On top of that, hardcoding timestamps wouldn’t exactly be a good practice for coding because of their length and complexity.

Fortunately, Python has a built-in ability to convert timestamps to time strings, without your having to first calculate the current timestamp. This is done with the datetime module.

The datetime module calculates the current timestamp for you and is used like this:


Using datetime.datetime.now(), Python automatically calculates the timestamp and then automatically converts it to a time string. For example, the output from a run just now prints:


You don’t have to stick with the above formatting because Python allows you to format the time however you want. This is achieved with the strtime() function and uses %Y, %m, %d, %H, and %D to achieve the formatting.

Here’s an example of formatting the time string. Let’s say you want your output to include the Month, Day, Year, Hour, and Minutes (which would be %m-%d-%Y %H:%M). The Python code for this would look like:

Syntax of datetime.fromtimestamp()

There’s another method of converting time, which takes a timestamp, converts it to a datetime object, and then stores date-time information for Python.

The syntax for this looks like the following:


Here’s an explanation as to what the above does:

  • datetime: This is the function that imports the necessary class for handling dates and times from Python’s built-in library.
  • int(your_timestamp_value): Replaces any placeholder with actual time data. This could be a string representing the time (e.g., “2025-07-23”) or an array of timestamps.
  • datetime.fromtimestamp(timestamp): This converts the number to a datetime object, which gives you an exact representation of the time.

You can use datetime.fromtimestamp() with timestamps. Of course, to do this, you would have to know the exact timestamp.

Let’s assume we’re using the timestamp for July 23, 2025, which is 1753280905. How do we use that with timestamp()? Like so:


The key benefit of using datetime.fromtimestamp() is that it seamlessly creates a structured representation of timestamps so they can be used within your Python code blocks.

Using the Python Time Module

The time module provides the necessary tools for dealing with time-related functions in Python. With the time module, you can easily get the current system time like so:


The output of the above would be the current Unix Epoch, such as:


There is also the sleep() function, which allows the addition of pauses in code. You might want to add a pause in your Python scripts so a process would execute at specific intervals or when performing time-sensitive operations. Here’s an example of the sleep() function:


What the above script would do is print “Waiting for 5 seconds…”, pause, and return your prompt. Or, you could add the following:


This would print “Waiting for five seconds…”, pause for five seconds, and then print “Five seconds have passed.”

Handling Timezones

Time zones add another layer of complexity to your code. You will need to manage time zones when working with data that spans different locales. If you don’t, the time could be wrong, depending on where the code is run.

With the Python datetime module, comes the tzinfo function, which allows you to work with time zones. Here’s an example:


Run the above code, and the results would look like this:


You could also convert from one time zone to another, like so:


The output of the above code would be (run at the current time):


And that’s how you start with Python timestamps and time strings.

Group Created with Sketch.
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.