Alejandro
Alejandro

Reputation: 571

Bash: get date and time from another time zone


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

Upvotes: 31

Views: 63993

Answers (5)

KJA
KJA

Reputation: 31

You may also add minutes using the following for Time Zones with half an hour difference:

date -d "-210 mins"

Upvotes: 1

mivk
mivk

Reputation: 15009

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

Upvotes: 11

rubo77
rubo77

Reputation: 20865

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

Upvotes: 5

anubhava
anubhava

Reputation: 785771

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

Upvotes: 16

SMA
SMA

Reputation: 37083

You could use

TZ=America/New_York date

or if you want to do date arithmetic you could use

date -d "+5 hours"

Upvotes: 50

Related Questions