2013/07/22

Converting from to unix time

I write this because I always forget the right formats, options...

BSD Date - Mac OSX

The f option is for input format. The j option is to not try to set the time. The last argument is optional, is the output format and it requires an x as a prefix.
  • Convert to unix time
    $ date -jf "%Y-%m-%d %H:%M:%S %Z" "2013-07-19 00:00:01 JST" "+%s"
    1374159601
    
  • Convert from unix time
    $ date -jf "%s" 1374159601 "+%Y-%m-%d %H:%M:%S %Z"
    2013-07-19 00:00:01 JST
    
    Also:
    $ date -jf "%s" 1374159601
    Fri Jul 19 00:00:01 JST 2013
    


GNU Date - Linux, etc

The -d is for the input date. Format is recognized automatically. The last parameter, same as BSD Date, is the output format (optional). It must be prepended by a +.
  • Convert to unix time
    $ date -d "2013-07-19 00:00:00 JST" "+%s"
    1374159600
    
  • Convert from unix time
    $ date -d @1374159600 "+%Y-%m-%d %H:%M:%S %Z"
    2013-07-19 00:00:00 JST
    
    Also :
    $ date -d @1374159600
    Fri Jul 19 00:00:00 JST 2013
    

0 comments :