linux

Linux Tip #2: Playing with dates

1 Apr 2005, posted on linux

Retrieving the current time under Unix is easy. Just use the date command:

$ date
Fri Apr  1 16:27:14 EST 2005

You can also use date formats to output the date in a specific way (so it can be used in scripts, or to form filenames):

$ date '+%Y%m%d-%H%M%S'
20050401-162855

But what happens if you want to see the GMT time, not the localtime? Just manipulate the TZ variable to fool date into believing we’re sitting on the GMT line:

$ TZ=GMT0 date
Fri Apr  1 21:30:21 GMT 2005

Another very frequent problem in scripts (specially log rotation scripts) is how to get yesterday’s date. Again, we can solve the problem by cleverly manipulating the TZ variable:

$ TZ=GMT24 date '+%Y%m%d'
20050331

Keywords: date, time, linux, yesterday, GMT, timezone, TZ


[Permalink] | [Digg Me] | [Add to del.icio.us] | [Submit to reddit] | [Submit to ma.gnolia.com] | [Submit to FURL]

[ 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 ] next ->>