0

I have tried to calculate number of days from January 1st to given date in same year. Option -d for UNIX command isn't working

date -d
date: illegal option -- d
Usage: date [-u] [+format]
       date [-u] [mmddhhmm[[cc]yy]]
       date [-a [-]sss.fff]

I'm using this script but is too long. Is there a simple way to calculate a nuber of days?

EDIT Result of a script:

$ ksh datecalc -a 2013 2 5 - 2013 1 1
$ 35
Cikson
  • 106
  • 2
  • 17
  • 2
    is `date '+%j'` helpful? – mpez0 Dec 23 '13 at 14:04
  • 1
    Older/vendor unix systems don't always support the `-d` option, as the usage message you receive back seems to indicate. You may find that the gnu-date utility which does support `-d` is installed in an alternate directory. Might help to add a tag for which Unix you are using. Good luck. – shellter Dec 23 '13 at 17:27

2 Answers2

1

OK so this may be a bit far fetched, but mysql client (or other DB clients) can come in handy for this as they have reliable and well documented date functions.

$ mysql ..... --silent -e "select datediff('2013-02-05', '2013-01-01') from dual;"

35

$

where ..... are your connection options.

geert3
  • 7,086
  • 1
  • 33
  • 49
  • I'm using Informix database. I have solved it with this: (http://www.prolifics.com/docs/panther4.6/html/dev_html/progjpl.htm#247139) – Cikson Dec 23 '13 at 14:07
0

If you have Perl installed, you can do:

perl -MPOSIX=strftime -le 'print strftime("%j",localtime)'

For a specific day, e.g. Feb 5:

perl -MPOSIX=strftime -le '
    @d=(2013,2,5); 
    print strftime("%j",0,0,0,$d[2],$d[1]-1,$d[0]-1900)
'
036
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • I have perl v5.6.1 installed on my machine but, when I run command result is: perl: Command not found. When I run script result is: syntax error at number_of_days.pl line 7, near "-le" Execution of number_of_days.pl aborted due to compilation errors. – Cikson Dec 24 '13 at 07:53
  • 1
    @Cikson: What is in number_of_days.pl? The `-le` is specified as a command-line option, not within a perl script. – cdarke Jan 02 '14 at 15:03
  • type -a perl /bin/type[6]: whence: A specified flag is not valid for this command. – Cikson Jan 07 '14 at 08:36