How Many Days in a Month/Year


A couple of essential calculations when dealing with date-based data.

First we need to know whether a year is a leap year...

      leap←{                    
          ⍝ Is this a leap year     
          1 3∊⍨+⌿0=4 100 400∘.|⍵}
      leap 2008 2009
1 0

Calculating the number of days in a year is then simple...

       daysinyear←{⎕IO ⎕ML←0 3
          ⍝ Number of days in a year
          365+leap ⍵}
       daysinyear 2008 2009
366 365

Slightly more complex to calculate the days in a month, but we also handle a couple of degenerate cases...

      daysinmonth←{⎕IO ⎕ML←1 3                             
         ⍝ How many days in a month                          
         0∊⍵:0                                           
         1=⍴,⍵:∇¨⍵,¨⍳12                                  
         (2⊃⍵)⊃31(28+leap↑⍵)31 30 31 30 31 31 30 31 30 31}
      daysinmonth 0
0
      daysinmonth 2009
31 28 31 30 31 30 31 31 30 31 30 31
      daysinmonth 2009 2
28  



Notes:

Page created 17 February 2009.
Copyright © Dogon Research 2009.