5

I am using the "date" command to perform some simple date arithmetic. The "-d" option lets me do this, but the man pages are very unclear on what is valid data. What are all the accepted options, and how do I figure that out?

I can use this command to determine the date 5 minutes ago: date -d "5-minutes-ago" +%H:%M

This command also works: date -d "-5 minutes" +%H:%M

I stumbled upon these options by accident/frustration. I don't see a "minutes-ago" or a "-X minutes" option listed in the documentation.

I would use Perl to do the arithmetic, but unfortunately I cannot install any additional Perl packages.

curious_george
  • 174
  • 1
  • 8
  • If you have `gawk`, it will do date arithmetic. – Dennis Williamson Mar 04 '11 at 18:15
  • You can do date aritmatic in bash/sh by keeping the dates in UNIX timestamps, just until you need to represent them to a user. Then the date command can convert the timestamp into a string (see example below) – JeffG Mar 04 '11 at 21:20

3 Answers3

6

There are a few relative date strings. minutes ago works (vs. minutes-ago with the dash).

There are quite a few examples on the coreutils date info pages:

http://www.gnu.org/software/coreutils/manual/html_node/Date-input-formats.html

Cakemox
  • 25,209
  • 6
  • 44
  • 67
2

A tricky one is the UNIX timestamp format (this is useful if you are managing a textfile database -- you want to keep your date/time data as UNIX timestamps so you can perform interger math on them)

$ date +%s
1299273353
$ date -d @1299273353
Fri Mar  4 15:15:53 CST 2011

The at-sign tells the date command that the number is a UNIX timestamp

JeffG
  • 1,194
  • 6
  • 18
1

You don't need to install additional packages to handle date math in Perl; just use time() to get an epoch offset, and localtime() to convert it to a useful date data structure.

Jeff Albert
  • 1,987
  • 9
  • 14