date Hints and Tips

The date(1) command is often useful, but some of its syntax is dangerously close to black magic. :)

These are rituals[esc]dbwacommand lines that I've had to use, in case somebody (future me included) needs them.

Timezone conversion

To convert from some timezone to your own:

date -d "[YYYY-MM-DD] HH:MM TZ"

if no day is specified the current day is used, e.g. (on 2012-04-04):

$ date -d "9:00 UTC"
Wed Apr  4 11:00:00 CEST 2012
$ date -d "2012-04-04 9:00 UTC"
Wed Apr  4 11:00:00 CEST 2012
$ date -d "2012-03-04 9:00 UTC"
Sun Mar  4 10:00:00 CET 2012

To convert from your (or any other) timezone to some other timezone:

TZ='DestTZ' date -d 'TZ="OrigTZ" [YYYY-MM-DD] HH:MM'

e.g.:

$ TZ=UTC date -d 'TZ="Europe/Rome" 9:00'
Wed Apr  4 07:00:00 UTC 2012
$ TZ=UTC date -d 'TZ="Europe/Rome" 2012-04-04 9:00'
Wed Apr  4 07:00:00 UTC 2012
$ TZ=UTC date -d 'TZ="Europe/Rome" 2012-03-04 9:00'
Sun Mar  4 08:00:00 UTC 2012

This also works:

$ TZ=UTC date -d "9:00 CEST"
Wed Apr  4 07:00:00 UTC 2012
$ TZ=UTC date -d "2012-04-04 9:00 CEST"
Wed Apr  4 07:00:00 UTC 2012

But you don't get automagical DST management:

$ TZ=UTC date -d "2012-03-04 9:00 CEST"
Sun Mar  4 07:00:00 UTC 2012

(this is formally correct, except that AFAIK no country was using CEST on 4 March).

Send a comment: unless requested otherwise I may add it, or some extract, to this page.

Return to Top