How to convert timestamps to dates in Bash ?

How to convert timestamps to dates in Bash ?

Asked on December 21, 2018 in Bash.
Add Comment


  • 3 Answer(s)

    We can use this simple code,

    date -d @1267619929
    

    This code runs on latest version of linux .

    Answered on December 21, 2018.
    Add Comment

    Try this code . This code works for ‘mac os x’ users.

    date -r <number>
    
    Answered on December 21, 2018.
    Add Comment

    This solution omits the need for the external tty and cat. So we can easily get the answer. It uses date, but could just as easily use gawk. one can change the shebang and replace the set of  single ones and double square brackets with and sh this will also run.

    #!/bin/bash
    LANG=C
    if [[ -z "$1" ]]
    then
        if [[ -p /dev/stdin ]] # input from a pipe
        then
            read -r p
        else
            echo "No timestamp given." >&2
            exit
        fi
    else
        p=$1
    fi
    date -d "@$p" +%c
    
    Answered on December 21, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.