Linux Commands

How Can I Generate UNIX Timestamps in Linux

In Unix timestamp we represent any date and time together in a single long number. This single number describes the total seconds passed since January 1st,1970 at 00:00:00 UTC. In this guide, we’ll cover everything about converting Unix timestamps to dates, including what Unix timestamps are, how to convert them to a date, and some practical examples to help you get started.

Content for this article is:

What is a Unix Timestamp

A Unix timestamp shows a single number which corresponds to date and time. This single number shows the total seconds that have passed since January 1st,1970 at 00:00:00 UTC. We also called this single number time the Unix epoch. Unix timestamps are used in many different programming languages to represent dates and times in any time zone.

How to Convert Unix Timestamp to Date

In Linux there are different methods to convert a Unix Timestamp to a date or vice versa. In this article we will start from the basic method which is using the date command in the terminal window.

  • Using date Command
  • Using the Perl Programming Language
  • Using the Python Programming Language
  • Using a Bash Script

Method 1: Using the date Command

To convert a Unix timestamp to a human-readable date and time, we can use the date command in the terminal. The syntax for the date command is as follows:

date -d @<unix_timestamp>

For example, to convert a Unix timestamp of 1676865654 to a human-readable date and time run the following command:

date -d @1676865654

As you can see, the output includes complete details of current date and time including time zone, and the year.

Similarly, we can also generate timestamp value for a specific date. For example, timestamp value for January 1, 2023, at 12:00:00 AM can be get using the following command:

date -d "2023-01-01 00:00:00" +%s

Following command will return UNIX timestamps for current date in nanoseconds format:

date +%s%N

Example 1: Convert a Unix Timestamp to a Date and Time in a Specific Time Zone

To convert a Unix timestamp to a date and time in a specific time zone, following command syntax will be followed:

TZ=<timezone> date -d @<unix_timestamp>

For example, to convert a Unix timestamp of 1613475901 to a human-readable date and time in the Eastern Time zone, run below command:

TZ=America/New_York date -d @1676865654

Example 2: Convert a Unix Timestamp to a Date Only

To convert a Unix timestamp to a date only, you can use the following command:

date -d @<unix_timestamp> +'%Y-%m-%d'

For example, if we have a Unix timestamp of 1613475901 and to convert it in the format of year-month-day (e.g. 2023-02-20) we can use following command:

date -d @ 1676865654 +'%Y-%m-%d'

This will output the date corresponding to the Unix timestamp of 1613475901:

Method 2: Using the Perl Programming Language

Perl is a popular programming language that can be used to generate UNIX timestamps in Linux.

Open a nano editor using:

nano

Now create a simple Perl script that generates a UNIX timestamp for the current date and time:

#!/usr/bin/perl
print time();

Press Ctrl + O, then save this script as “timestamp.pl” and hit Enter, then press Ctrl + X to save and exit:

Now make this executable by running:

chmod +x timestamp.pl

Run the script with the command “./timestamp.pl” to generate a UNIX timestamp:

Method 3: Using the Python Programming Language

Python is another popular programming language that can be used to generate UNIX timestamps in Linux.

First, we have to install Python3 on Linux, to do that run command:

sudo apt install python3

Now open nano editor using:

nano

Create a simple Python script that generates a UNIX timestamp for the current date and time:

#!/usr/bin/python3
import time
print(int(time.time()))

Press Ctrl + O, then save this script as “timestamp.py” and hit Enter, then press Ctrl + X to save and exit.

The above script can be made executable by below command:

chmod +x timestamp.py

Run the script with the command “./timestamp.py” to generate a UNIX timestamp.

Method 4: Using a Bash Script

If you need to generate UNIX timestamps in a more complex or automated way, you can use a Bash script.

Open the nano editor using:

nano

Write following script in editor that generates a UNIX timestamp for the current date and time:

#!/bin/bash
echo $(date +%s)

Press Ctrl + O, then save this script as “timestamp.sh” and hit Enter, then press Ctrl + X to save and exit:

Now make this script executable by running the command:

chmod +x timestamp.sh

Run the script with the command “./timestamp.sh” to generate a UNIX timestamp:

Conclusion

UNIX timestamps show total seconds passed since January 1, 1970. To generate UNIX timestamps in Linux, the date command can be used in the command line by passing the +%s argument. Alternatively, we can also create a bash script or use the pearl and python language script to give us timestamps for exact date and time zone.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.