Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a command line calendar calculation which provides these features?

  • Date (plus|minus) date interval calculation
  • User specified input and output format
share|improve this question
Use any programming language? Python is nice. – Zoredache Oct 26 '12 at 23:27
up vote 7 down vote accepted

The date command will do much of this for you.

For example, November 1 plus 3 weeks is date -j -v+3w -f"%m/%d/%y" "11/01/10"

Here are some examples from the man page:

The command:

 date "+DATE: %Y-%m-%d%nTIME: %H:%M:%S"

will display:

 DATE: 1987-11-21
 TIME: 13:36:16

In the Europe/London timezone, the command:

 date -v1m -v+1y

will display:

 Sun Jan  4 04:15:24 GMT 1998

where it is currently Mon Aug 4 04:15:24 BST 1997.

The command:

 date -v1d -v3m -v0y -v-1d

will display the last day of February in the year 2000:

 Tue Feb 29 03:18:00 GMT 2000

So will do the command:

 date -v30d -v3m -v0y -v-1m

because there is no such date as the 30th of February.

The command:

 date -v1d -v+1m -v-1d -v-fri

will display the last Friday of the month:

 Fri Aug 29 04:31:11 BST 1997

where it is currently Mon Aug 4 04:31:11 BST 1997.

The command:

 date 0613162785

sets the date to ``June 13, 1985, 4:27 PM''.

 date "+%m%d%H%M%Y.%S"

may be used on one machine to print out the date suitable for setting on another.

The command:

 date 1432

sets the time to 2:32 PM, without modifying the date.

Finally the command:

 date -j -f "%a %b %d %T %Z %Y" "`date`" "+%s"

can be used to parse the output from date and express it in Epoch time.

share|improve this answer
I think your examples are mostly for BSD date only. The -v option isn't available with GNU date (for version 8.5 at least). – donothingsuccessfully Oct 27 '12 at 18:49
Good point. GNU date has the --date option and a different syntax. – Alan Shutko Oct 27 '12 at 21:08

Like Zoredache says, you can easily achieve it using any scripting programming language.

Here is an example in Ruby

 > irb
 1.9.3-p286 :001 > require 'date'
  => true
 1.9.3-p286 :014 >,11,1) -,1,2)                                                                                                                                                                                                                    
  => (304/1) 
 1.9.3-p286 :015 >,11,1) + 10
  => #<Date: 2012-11-11 ((2456243j,0s,0n),+0s,2299161j)> 
 1.9.3-p286 :016 >,11,1) - 205
  => #<Date: 2012-04-10 ((2456028j,0s,0n),+0s,2299161j)> 
share|improve this answer

As for custom input and output formats, check out dateutils. It provides the dadd command to do the Date (plus|minus) date interval calculation:

$ dadd -i '%B %d %Y' 'November 1 2012' +3w
$ dadd 2012-11-22 -3w -f '%B %d %Y'
November 01 2012

The -i|--input-format option works throughout all tools and supports most of strptime(3) specifiers. Same for the -f|--format option.

Disclaimer: I am the author of the toolset.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .