Saturday, December 22, 2012

Shell script convert date to epoch or unix time.

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.

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


-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.


Post a Comment