Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
    
If you have gawk, it will do date arithmetic. –  Dennis Williamson Mar 4 '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 4 '11 at 21:20

3 Answers 3

up vote 4 down vote accepted

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

share|improve this answer

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.

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.