18


I would want to get the date and time from another time zone (UTC-4) with a bash command, but I don't want to configure it as the default TZ for the system.
Is there a simple way to do it?
E.g 1 (current):

$ date
fri nov  7 13:15:35 UTC 2014

E.g 2 (what I need):

$ date (+ some option for UTC-4)
fri nov  7 09:15:35 UTC 2014
tripleee
  • 175,061
  • 34
  • 275
  • 318
Alejandro
  • 434
  • 1
  • 3
  • 13

4 Answers4

34

You could use

TZ=America/New_York date

or if you want to do date arithmetic you could use

date -d "+5 hours"
SMA
  • 36,381
  • 8
  • 49
  • 73
  • Thank you @almas shaikh, the first option is the most accurate response for my case. – Alejandro Nov 07 '14 at 13:46
  • 5
    But it requires you to know the proper name of the time zone you want, which can be tricky to find out sometimes. You can also use a numeric offset, though it's by no means obvious how. The [timezone(3)](http://linux.die.net/man/3/timezone) manual page has the gory details. – tripleee Nov 07 '14 at 13:50
  • 1
    Also maybe check [Wikipedia](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) to find e.g. that `TZ=Asia/Kolkata` is one way to get IST (which is tricky because `TZ=UTC-5:30` isn't entirely obvious; e.g. `UTC-5.5` doesn't work). – tripleee Nov 02 '18 at 07:35
  • 1
    Any way this could be achieved for macOS? `-d` argument is for `daylight saving offset` in the date tool and there is no argument for date string. – Himanshu Tanwar May 04 '20 at 09:10
  • Doesn't work on Mac OS. This `TZ=UTC date -R` does work though. – james-see Jun 14 '20 at 05:12
  • For time zones with hours and minutes + or - UTC, eg for India: `date -d "+5 hours 30 minutes"` – Mohammad Yasir K P Jun 28 '22 at 06:20
11

You can use offset value in TZ to get the date for a different timezone:

TZ=UTC date -R
Fri, 07 Nov 2014 13:55:07 +0000

TZ=UTC+4 date -R
Fri, 07 Nov 2014 09:54:52 -0400
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Yes that is how `TZ` works with offset. I would suggest using offset since it doesn't require you to remember long & valid timezone names. – anubhava Nov 07 '14 at 13:50
  • 2
    You don't need a subshell. `TZ=UTC date` sets `TZ`, runs `date`, and reverts the value of `TZ` to its previous value (unset, or different value, or the same value as the one you specified, as the case may be). – tripleee Nov 07 '14 at 13:51
  • 2
    Also, `TZ=UTC-4 date` confusingly prints the time zone as "UTC" even though it's not. (This is visible in your example output, too.) – tripleee Nov 07 '14 at 13:52
  • Also better to use `date -R` for `--rfc-2822` format which displays correct offset as well. – anubhava Nov 07 '14 at 13:55
  • 1
    The `TZ=UTC date -R` is the only solution I could get to work that works on Mac OS for a redis set key insert I am doing and wanted UTC: `redis-cli set starttime "$(TZ=UTC date -R)"` – james-see Jun 14 '20 at 05:11
8

I use a little script for that, so I don't have to know or remember the exact name of the timezone I'm looking for. The script takes a search string as argument, and gives the date and time for any timezone matching the search. I named it wdate.

#!/bin/bash

# Show date and time in other time zones

search=$1

format='%a %F %T %z'
zoneinfo=/usr/share/zoneinfo/posix/

if command -v timedatectl >/dev/null; then
    tzlist=$(timedatectl list-timezones)
else
    tzlist=$(find -L $zoneinfo -type f -printf "%P\n")
fi

grep -i "$search" <<< "$tzlist" \
| while read z
  do
      d=$(TZ=$z date +"$format")
      printf "%-32s %s\n" "$z" "$d"
  done

Example output:

$ wdate fax
America/Halifax                  Fri 2022-03-25 09:59:02 -0300

or

$ wdate canad
Canada/Atlantic                  Fri 2022-03-25 10:00:04 -0300
Canada/Central                   Fri 2022-03-25 08:00:04 -0500
Canada/Eastern                   Fri 2022-03-25 09:00:04 -0400
Canada/Mountain                  Fri 2022-03-25 07:00:04 -0600
Canada/Newfoundland              Fri 2022-03-25 10:30:04 -0230
Canada/Pacific                   Fri 2022-03-25 06:00:04 -0700
Canada/Saskatchewan              Fri 2022-03-25 07:00:04 -0600
Canada/Yukon                     Fri 2022-03-25 06:00:04 -0700
mivk
  • 13,452
  • 5
  • 76
  • 69
3

If the other solutioons don't work, use

date --date='TZ="UTC+4" 2016-08-22 10:37:44' "+%Y-%m-%d %H:%M:%S"

2016-08-22 16:37:44

rubo77
  • 19,527
  • 31
  • 134
  • 226