DEV Community

Dmitry Romanoff
Dmitry Romanoff

Posted on

2

Bash script to find the difference between an epoch timestamp and the current date.

Bash script to find the difference between an epoch timestamp and the current date.

Here's an example Bash script to find the difference between an epoch timestamp and the current date:

#!/bin/bash

# Enter the epoch timestamp in seconds
epoch_timestamp=1649512800

# Get the current date in seconds since epoch
current_date=$(date +%s)

# Calculate the difference between the two timestamps
difference=$((current_date - epoch_timestamp))

# Print the difference in seconds, minutes, hours, and days
echo "Difference in seconds: $difference"
echo "Difference in minutes: $(($difference / 60))"
echo "Difference in hours: $(($difference / 3600))"
echo "Difference in days: $(($difference / 86400))"
Enter fullscreen mode Exit fullscreen mode

Replace 1649512800 with the epoch timestamp you want to compare with the current date. The script will output the difference in seconds, minutes, hours, and days between the two timestamps.

dmi@dmi-laptop:~$ cat my_script.sh 
#!/bin/bash

# Enter the epoch timestamp in seconds
epoch_timestamp=1649512800
echo "epoch_timestamp: $(date -d "@${epoch_timestamp}" "+%Y %m %d %H %M %S")"

# Get the current date in seconds since epoch
current_date=$(date +%s)
echo "current_date: $(date -d "@${current_date}" "+%Y %m %d %H %M %S")"

# Calculate the difference between the two timestamps
difference=$((current_date - epoch_timestamp))

# Print the difference in seconds, minutes, hours, and days
echo "Difference in seconds: $difference"
echo "Difference in minutes: $(($difference / 60))"
echo "Difference in hours: $(($difference / 3600))"
echo "Difference in days: $(($difference / 86400))"
Enter fullscreen mode Exit fullscreen mode

The script first finds the current epoch timestamp using the date +%s command. It then finds the difference between the two timestamps in seconds and prints it.

The script uses the date -d command to convert the epoch timestamp to the desired format.

The output will look like this:

dmi@dmi-laptop:~$ ./my_script.sh 
epoch_timestamp: 2022 04 09 17 00 00
current_date: 2023 04 10 19 23 09
Difference in seconds: 31630989
Difference in minutes: 527183
Difference in hours: 8786
Difference in days: 366
dmi@dmi-laptop:~$ 
Enter fullscreen mode Exit fullscreen mode

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay