Q. How can I convert a given date in to epoch or UNIX time format which are in seconds using a Linux Shell script?

date to epoch seconds can be achieved by using date command. Below is the script which asks user for an input and convert it to UNIX time format.

#!/bin/bash
read -p “Please enter a date in YYYY-MM-DD format: ” DATE1
echo “The UNIX time for $DATE1 is $(date -d”$DATE1″ +%s)”

Save above file and change the permissions.

chmod +x date2epoch.bash

./date2epoch.bash

-d is used to mention the date to get converted.
+%s is to convert this date to epoch or UNIX time-stamp.

This is the simplest way to get the date converted to UNIX seconds.

The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.