date
(1) CommandNeed to do date arithmetic in Linux? Adjusting a date variable using an offset of the current time in seconds can make date math much more convenient.
To obtain the current time in seconds since the epoch use, "date +%s
".
The standard UNIX epoch is defined as: 00:00:00 UTC, January 1, 1970
.
Here is an example of obtaining the current time in "Seconds since the epoch":
# date ; echo -n "Seconds since the epoch: " ; date +%s
Mon Dec 22 08:48:31 MST 2008
Seconds since Epoch: 1229960911
To convert a number of seconds since Epoch back in to something human readable, replace "+1229960911
" in the next example with the epoch number of seconds with which you wish to work.
# date -d '1970-01-01 UTC +1229960911 seconds'
Mon Dec 22 08:48:31 MST 2008
The BSD (Berkeley Software Distribution) version of the date
command (i.e., the version that is installed on Mac OS X) can accomplish the same task, though with slightly different command line options.
# date -jf %s 1229960911
Mon Dec 22 10:48:31 EST 2008
There may be times where you need to find a date for things like "three months from the current date" or "one week earlier".
This type of activity can be returned using the "-d
string" option of the date
command as was seen in the previous example (where string defines the format of date).
This is where you can implement some rudimentary math.
For example, you can find tomorrow's date using "+1 day
" as the string.
# date; date -d "+1 day"
Mon Dec 22 09:21:09 MST 2008
Tue Dec 23 09:21:09 MST 2008
The format of the string is "+/-
n unit where n is the number of units and unit is any one of following types:
The BSD version of the date
command uses one or more "-v [+|-]val[ymwdHMS]]
" options. For example a BSD equivalent on OS X would be:
# date -v +1d
Tue Dec 23 14:31:04 EST 2008
Wed Dec 24 14:31:04 EST 2008
# date; date -d "+12 hour"
Mon Dec 22 09:22:17 MST 2008
Mon Dec 22 21:22:17 MST 2008
# date; date -d "-3 month"
Mon Dec 22 09:22:47 MST 2008
Mon Sep 22 10:22:47 MDT 2008
# date; date -d "+1 month -3 hour"
Mon Dec 22 09:24:25 MST 2008
Thu Jan 22 06:24:25 MST 2009
The BSD equivalent on OS X:
# date; date -v +1m -v -3H
Tue Dec 23 14:37:43 EST 2008
Fri Jan 23 11:37:43 EST 2009
Adding an output format option to date will return dates which may be easier to use in your scripts and commands.
Both dates in the next example show the date one month from when the command was issued, but the second date adds formatting option "+%Y%m%d
"
to the command to return the date in YYYYMMDD
format which may be preferred in scripts.
# date -d "+1 month"; date -d "+1 month" +%Y%m%d
Thu Jan 22 09:29:26 MST 2009
20090122
Additional information regarding the the various formatting options available with the date
command on your system, see the date
(1) reference page and the Texinfo page (i.e., info coreutils date
).
If you find yourself in an environment where a similar solution is needed that works consistently across multiple platforms, we would recommend writing a script in a language like Perl, Python, or Ruby.
Or, if your other operating systems are UNIX-based you could port the Linux [or BSD] date
command as another option.
We hope that you've found this tip useful. If you have any questions, comments, or suggestions for future tips, please contact us.