# Leap Year

This extrinsic function will return 1 if YEAR is a leap year; 0 if not:

```LEAPYEAR(YEAR)
N RETVAL S RETVAL=0
I YEAR#400=0 S RETVAL=1
I YEAR#100=0 S RETVAL=0
I YEAR#4=0 S RETVAL=1
Q RETVAL```

# Day of Week

This extrinsic function will return the day of the week for DATE as an integer from 0 through 6, where 0 is Sunday:

```DAYOFWEEK(DATE)
N MONTH,DAY,NEWYEAR,YEAR,CENT,DMY
S MONTH=\$P(DATE,"/","1")
S DAY=\$P(DATE,"/","2")
S YEAR=\$P(DATE,"/","3")
I MONTH<3 S MONTH=MONTH+12,YEAR=YEAR-1
S MONTH=MONTH+1
S MONTH=MONTH*2.61
S MONTH=\$P(MONTH,".",1)
S NEWYEAR=YEAR
S YEAR=\$E(NEWYEAR,3,4)
S DMY=DAY+MONTH+YEAR
S CENT=\$E(NEWYEAR,1,2)
S YEAR=YEAR/4
S YEAR=\$P(YEAR,".",1)
S DMY=DMY+YEAR
I CENT=18 S CENT=2
I CENT=19 S CENT=0
I CENT=20 S CENT=6
I CENT=21 S CENT=4
S DMY=DMY+CENT
Q DMY#7
```

# Date Within

This extrinsic function and supporting GT.M implementation-specific function EDH will return 1 if INPUTDATE is within DATE1 and DATE2 (inclusive):

```DATEWITHIN(INPUTDATE,DATE1,DATE2)
D NORMALIZE(.INPUTDATE)
D NORMALIZE(.DATE1)
D NORMALIZE(.DATE2)
N IDH,D1H,D2H S (IDH,D1H,D2H)=0
S IDH=\$\$EDH(INPUTDATE),D1H=\$\$EDH(DATE1),D2H=\$\$EDH(DATE2)
Q (IDH'<D1H)&(IDH'>D2H)
;
EDH(DATE)
Q \$\$FUNC^%DATE(DATE)```

## 3 thoughts on “Date Calculations in MUMPS”

1. In LEAP YEAR, lines 3 and 5 should be reversed.

1. As an optimization? Would it also be good to skip the following two IF constructs?

2. No, not an optimisation. As written, the function will always return 1 where the year is evenly divisible by 4 which, I assume, isn’t the desired effect.