How to convert a date string to the current time zone on the command line?

11

4

How can I convert a date/time string, e.g. 2011-11-13 08:11:02 +0000, to my local time zone on the command line?

Daniel Beck

Posted 2011-11-13T08:34:47.797

Reputation: 98 421

Don't hesitate to add different answers, but please remember that OS X has BSD date, not GNU date. The options are quite different. – Daniel Beck – 2011-11-13T08:45:11.993

Answers

17

Use date -jf "<input format>" "<input value>" +"<output format>".

The following converts a UTC date to my local time zone (CET):

$ date -jf "%Y-%m-%d %H:%M:%S %z" "2011-11-13 08:11:02 +0000" +"%Y_%m_%d__%H_%M_%S"
2011_11_13__09_11_02

If you specify the same date format for both input and output, you will only convert the time to your local time zone. But, as in the example, you can combine that with a format conversion.

Daniel Beck

Posted 2011-11-13T08:34:47.797

Reputation: 98 421

2The target time zone can be changed by temporarily setting $TZ: TZ=EST date -jf %H%z 23+0100 +%I%p05PM – Lri – 2011-11-13T20:07:29.550