xyzzy
xyzzy

Reputation: 91

linux date command convert arbitrary date time to utc

How do I convert an arbitrary date and time to UTC using the 'date' command in Linux?

Using the following format just prints my specified time and doesn't convert it.

# /usr/bin/date -u --date="2016-06-21 01:00:00"
Tue Jun 21 01:00:00 UTC 2016

Using the current system time does seem to convert to UTC correctly

# /usr/bin/date
Tue Jun 21 08:48:44 EDT 2016
#/usr/bin/date -u 
Tue Jun 21 12:48:55 UTC 2016

Upvotes: 4

Views: 10930

Answers (2)

edi_allen
edi_allen

Reputation: 1872

You can use command substitution to get your desired output. The command inside parentheses will return the number of seconds since epoch. Then date -u --date will format the date to UTC.

date -u --date=@$(date "+%s" --date="2016-06-21 01:00:00")
Tue Jun 21 05:00:00 UTC 2016

The above command will convert an arbitrary date and time to UTC as it is required.

Upvotes: 5

fedorqui
fedorqui

Reputation: 289725

If you want to have the date in UTC, prepend the region:

$ date
Tue Jun 21 15:00:10 CEST 2016
$ TZ=UTC date
Tue Jun 21 13:00:10 UTC 2016

If you want to use a specific date, just use -d normally:

$ TZ=UTC date -d"yesterday"
Mon Jun 20 13:00:55 UTC 2016

Upvotes: 3

Related Questions