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

  • Date (plus|minus) date interval calculation
  • User specified input and output format
  • 1
    Use any programming language? Python is nice.
    – Zoredache
    Oct 26 '12 at 23:27

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.

  • 3
    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). Oct 27 '12 at 18:49
  • 2
    Good point. GNU date has the --date option and a different syntax. 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 > Date.new(2012,11,1) - Date.new(2012,1,2)                                                                                                                                                                                                                    
  => (304/1) 
 1.9.3-p286 :015 > Date.new(2012,11,1) + 10
  => #<Date: 2012-11-11 ((2456243j,0s,0n),+0s,2299161j)> 
 1.9.3-p286 :016 > Date.new(2012,11,1) - 205
  => #<Date: 2012-04-10 ((2456028j,0s,0n),+0s,2299161j)> 

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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