Convert TZ output to a different format

Friends,

I am trying to convert my local server timezone EST to UTC and for which I used the TZ command, see below

$ date
Thu Dec  6 10:14:14 EST 2012
$
$ TZ=UTC date -d '10:14 EST'
Thu Dec  6 15:14:00 UTC 2012

Now I would like to have the same output in 'yyyymmdd hh:mm' format.

Can anyone help?

Thanks

Hi,
just add the format string to your statement. man date should tell you the possible formating options.

$ TZ=UTC date -d '10:14 EST' '+%Y%m%d %H:%M'
20121206 15:14

Thanks Cero.

What if that time is a variable. Say in the above example '10:14' is a variable, how can I pass it in the TZ command. I tried, but it gave an error message :frowning:

$ x=$(date +%H:%M)
$ echo $x
10:40
$ TZ=UTC date -d '${x} EST' '+%Y%m%d %H:%M'
date: invalid date `${x} EST'

Appreciate your help.

Thats because the single quotes prevent variable substitution. Use double quotes instead:

$ x=$(date +%H:%M)
$ echo $x
16:46
$ TZ=UTC date -d "${x} EST" '+%Y%m%d %H:%M'
20121206 21:46
1 Like

Perfect! that worked. Thanks Cero.