Two-way conversion of Unix time (seconds since 1970) and regular time

I found using of Unix time to be very useful in various shell scripts and here are two simple commands to convert Unix/Linux date command to Unix time format and back to regular formating:

To convert Unix time to simple (regular) time please use:

date -u --date="1970-01-01 1187769064 sec GMT"

where 1187769064 is input Unix time. The output will be: Wed Aug 22 07:51:04 UTC 2007

To get Unix time seconds from regular one format just use:

date --date="Wed Aug 22 07:51:04 UTC 2007" +%s

where Wed Aug 22 07:51:04 UTC 2007 is input regular time. The output will be: 1187769064.

Update: another way to convert Unix time into regular date is to use the following command date -d @1187769064 (thanks to Mattias Lindvall), that is tested in Ubuntu and Fedora.

SHARE:
nv-author-image

Stefan Durand

My name is Stefan, I'm the admin of LinuxScrew. I am a full-time Linux/Unix sysadmin, a hobby Python programmer, and a part-time blogger. I post useful guides, tips, and tutorials on common Linux and Programming issues. Feel free to reach out in the comment section.

11 thoughts on “Two-way conversion of Unix time (seconds since 1970) and regular time”

  1. date +%s gives you unix-time

    date -d @123456890 converts back.

    simple way to test

    date -d @$(date +%s) (this basicaly takes the output from "date +%s" as input for "date -d@" and thus should print the current time in human readable form)

  2. scriptify! This will function as the ‘date’ command, but with first param being the “epoch seconds” (allowing all the date command reformatting options):

    #!/bin/bash
    s=$1
    shift
    date -d @$s “$@”

  3. In FreeBSD convert from date to unixtime:

    date -j -f “%a %b %d %T %Z %Y” “`date`” “+%s”
    can be used to parse the output from date and express it in Epoch time.

    Or for any date:
    utm utm # date -j “201209252359.00” “+%s”
    1348603140
    utm utm # date -r 1348603140
    ???????, 25 ???????? 2012 ?. 23:59:00 (MSK)

Leave a Reply

Your email address will not be published. Required fields are marked *