Date Calculations

Location:   http://www.mvps.org/dmcritchie/excel/datecalc.htm
Home page: http://www.mvps.org/dmcritchie/excel/excel.htm
[View without Frames]

ABCDE
Dates(Dates are shown in US/Canada format mm/dd/yyyy) Formula used
starting date01/19/19982Mon=WEEKDAY(B3,1)
add days18207/20/1998Mon=B3+B4
add months607/19/1998Sun=DATE(YEAR(B3),MONTH(B3)+B5,DAY(B3))
add years101/30/1999Sat=DATE(YEAR(B3)+1,MONTH(B3),DAY(B4))
     
Age Calculation   Q129277 XL: Undocumented Worksheet Function DATEDIF
Anniv/Birthdate06/01/1968Example 24990
Years of Age30  =DATEDIF(B9,NOW(),"Y")
+ Months2  =DATEDIF(B9,NOW(),"YM")
+ Days14  =DATEDIF(B9,NOW(),"MD")
Todays Date08/15/1998  =NOW()
Days after Last Birthdate75  =DATEDIF(B9,NOW(),"YD")
     
More Date Calculations    
  Day of Wk.Day of Wk. 
date chosen03/15/19992Mon36234
  =WEEKDAY(B18,1)  
   =B18 
First ddd of Month03/01/19992Mon=DATE(YEAR(B18),MONTH(B18),1)
Last ddd of Month03/31/19994Wed=DATE(YEAR($B$18),MONTH($B$18)+1,1)-1
First Sunday of Mo.03/07/19991Sun=DATE(YEAR($B$18),MONTH($B$18),1)+ MOD(1+7-WEEKDAY(DATE(YEAR($B$18),MONTH($B$18),1)),7)
First Monday03/01/19992Mon=DATE(YEAR($B$18),MONTH($B$18),1)+ MOD(2+7-WEEKDAY(DATE(YEAR($B$18),MONTH($B$18),1)),7)
First Tuesday03/02/19993Tue=DATE(YEAR($B$18),MONTH($B$18),1)+ MOD(3+7-WEEKDAY(DATE(YEAR($B$18),MONTH($B$18),1)),7)
First Wednesday03/03/19994Wed=DATE(YEAR($B$18),MONTH($B$18),1)+ MOD(4+7-WEEKDAY(DATE(YEAR($B$18),MONTH($B$18),1)),7)
First Thursday03/04/19995Thu=DATE(YEAR($B$18),MONTH($B$18),1)+ MOD(5+7-WEEKDAY(DATE(YEAR($B$18),MONTH($B$18),1)),7)
First Friday03/05/19996Fri=DATE(YEAR($B$18),MONTH($B$18),1)+ MOD(6+7-WEEKDAY(DATE(YEAR($B$18),MONTH($B$18),1)),7)
First Saturday03/06/19997Sat=DATE(YEAR($B$18),MONTH($B$18),1)+ MOD(7+7-WEEKDAY(DATE(YEAR($B$18),MONTH($B$18),1)),7)
Last Sunday of Mo.03/28/19991Sun=DATE(YEAR($B$18),MONTH($B$18)+1,1)- WEEKDAY(DATE(YEAR($B$18),MONTH($B$18)+1,7))
Last Monday03/29/19992Mon=DATE(YEAR($B$18),MONTH($B$18)+1,1)- WEEKDAY(DATE(YEAR($B$18),MONTH($B$18)+1,6))
Last Tuesday03/30/19993Tue=DATE(YEAR($B$18),MONTH($B$18)+1,1)- WEEKDAY(DATE(YEAR($B$18),MONTH($B$18)+1,5))
Last Wednesday03/31/19994Wed=DATE(YEAR($B$18),MONTH($B$18)+1,1)- WEEKDAY(DATE(YEAR($B$18),MONTH($B$18)+1,4))
Last Thursday03/25/19995Thu=DATE(YEAR($B$18),MONTH($B$18)+1,1)- WEEKDAY(DATE(YEAR($B$18),MONTH($B$18)+1,3))
Last Friday03/26/19996Fri=DATE(YEAR($B$18),MONTH($B$18)+1,1)- WEEKDAY(DATE(YEAR($B$18),MONTH($B$18)+1,2))
Last Saturday03/27/19997Sat=DATE(YEAR($B$18),MONTH($B$18)+1,1)- WEEKDAY(DATE(YEAR($B$18),MONTH($B$18)+1,8))
=offset(cellname,-1,0)27.03.19997Sat=OFFSET(B37,-1,0)
Last weeknum of yr53  =WEEKNUM(DATE(YEAR(B18),12,31),1)
This page was intended as a summary of calculations on my datetime page, but some of the formulas shown here differ, I think the nth weekday day of Month formulas are easier to follow at First, Last, Nearest, Closest, nth weekday (4th Monday) in Month, Date calculations on my Date and Time page.
Chip Pearson has UDF on his pages to find the nth day of week for a month in a year see example then go to his page from there.
First Wednesday (4=Wed) of the month during month of 03/03/1999
  =DATE(YEAR($B$18),MONTH($B$18),1)+ MOD(4+7-WEEKDAY(DATE(YEAR($B$18),MONTH($B$18),1)),7)
 add   7 to the above for the  2nd Wed
 add 14 to the above for the  3rd Wed
 add 21 to the above for the  4th Wed
 the 5th Wed add 28 if it is still in same month it is valid
   =IF(Month($B$18)<>Month(formula+28),"",formula+28)

=IF(Month($B$18)<>Month(DATE(YEAR($B$18),MONTH($B$18),1)+ MOD(4+7-WEEKDAY(DATE(YEAR($B$18),MONTH($B$18),1)),7)+28),"",DATE(YEAR($B$18),MONTH($B$18),1)+ MOD(4+7-WEEKDAY(DATE(YEAR($B$18),MONTH($B$18),1)),7)+28)

January, 2002 has a 5th Wednesday and it is January 30, 2002.  Except in a Leap Year, February does not have a 5th Wednesday or any other day of week in the month.

WORKDAY Worksheet Function

Formulas and Formats used to create table for the nth working day after the last day of previous month: 
Workday is an Analysis Toolpak addin, simply activate it under Tools.
=WORKDAY("10/01/2001"-1,ROW())     formatted as mm/dd/yyyy
=WORKDAY("11/01/2001"-1,ROW())
=WORKDAY("12/01/2001"-1,ROW())

=ROW()           formatted as General

=WEEKDAY(A1)     formatted as ddd
 ABCDEFGHIJK
110/01/20011Mon  11/01/20011Thu  12/03/20011Mon
210/02/20012Tue  11/02/20012Fri  12/04/20012Tue
310/03/20013Wed  11/05/20013Mon  12/05/20013Wed
410/04/20014Thu  11/06/20014Tue  12/06/20014Thu
510/05/20015Fri  11/07/20015Wed  12/07/20015Fri
610/08/20016Mon  11/08/20016Thu  12/10/20016Mon
710/09/20017Tue  11/09/20017Fri  12/11/20017Tue
810/10/20018Wed  11/12/20018Mon  12/12/20018Wed
910/11/20019Thu  11/13/20019Tue  12/13/20019Thu
1010/12/200110Fri  11/14/200110Wed  12/14/200110Fri
1110/15/200111Mon  11/15/200111Thu  12/17/200111Mon
1210/16/200112Tue  11/16/200112Fri  12/18/200112Tue
1310/17/200113Wed  11/19/200113Mon  12/19/200113Wed
1410/18/200114Thu  11/20/200114Tue  12/20/200114Thu
1510/19/200115Fri  11/21/200115Wed  12/21/200115Fri
1610/22/200116Mon  11/22/200116Thu  12/24/200116Mon
1710/23/200117Tue  11/23/200117Fri  12/25/200117Tue
1810/24/200118Wed  11/26/200118Mon  12/26/200118Wed
1910/25/200119Thu  11/27/200119Tue  12/27/200119Thu
2010/26/200120Fri  11/28/200120Wed  12/28/200120Fri
2110/29/200121Mon  11/29/200121Thu  12/31/200121Mon
2210/30/200122Tue  11/30/200122Fri  01/01/200222Tue
2310/31/200123Wed  12/03/200123Mon  01/02/200223Wed

The dates above were chosen to include a month that begins on a Saturday (Dec 1, 2001), which is not a workday. 

For illustrative purposes only.  Holidays were not included as a parameter in WORKDAY so Christmas (Dec 25), which is a US holiday is shown as a workday.
  to work with holidays use   =WORKDAY(start_date,days,holidays)
i.e. =WORKDAY("12/01/2001"-1,20,{"12/24/01","12/25/2001"})

First, 2nd, 3rd, 4th, 5th, named weekday, on or after a date

 ABCDEFGHIJ
1Date Weekday 1st-5th
on or after
calc. Date Day of week verify occur-
rence
  Calc within Month
2 INPUT Calc. Reference INPUT Calculated Calc. Reference   Calculated
311/1/2002 6 Fri 3 1 11/05/2002 3 1  11/05/2002
411/1/2002 6 Fri 3 2 11/12/2002 3 2  11/12/2002
511/1/2002 6 Fri 3 3 11/19/2002 3 3  11/19/2002
611/1/2002 6 Fri 3 4 11/26/2002 3 4  11/26/2002
711/1/2002 6 Fri 3 5 12/03/2002 3 1  --
812/1/2002 1 Sun 3 1 12/03/2002 3 1  12/03/2002
912/1/2002 1 Sun 3 2 12/10/2002 3 2  12/10/2002
1012/1/2002 1 Sun 3 3 12/17/2002 3 3  12/17/2002
1112/1/2002 1 Sun 3 4 12/24/2002 3 4  12/24/2002
1212/1/2002 1 Sun 3 5 12/31/2002 3 5  12/31/2002
13            
1412/2/2002 2 Mon 3 1 12/03/2002 3 1  12/03/2002
1512/3/2002 3 Tue 3 1 12/03/2002 3 1  12/03/2002
1612/4/2002 4 Wed 3 1 12/10/2002 3 2  12/10/2002
1712/5/2002 5 Thu 3 1 12/10/2002 3 2  12/10/2002
1812/6/2002 6 Fri 3 1 12/10/2002 3 2  12/10/2002
1912/7/2002 7 Sat 3 1 12/10/2002 3 2  12/10/2002
2012/8/2002 1 Sun 3 1 12/10/2002 3 2  12/10/2002
2112/9/2002 2 Mon 3 1 12/10/2002 3 2  12/10/2002
2212/14/2002 7 Sat 3 1 12/17/2002 3 3  12/17/2002
2312/15/2002 1 Sun 3 1 12/17/2002 3 3  12/17/2002
2412/16/2002 2 Mon 3 1 12/17/2002 3 3  12/17/2002
2512/17/2002 3 Tue 3 2 12/24/2002 3 4  12/24/2002
2612/18/2002 4 Wed 3 3 01/07/2003 3 1  --
2712/19/2002 5 Thu 3 3 01/07/2003 3 1  --
28 Calculate the First, Third, Fifth Wednesday within the month
293/1/2003   4 1      03/05/2003
303/1/2003   4 3      03/19/2003
313/1/2003   4 5      --
32            
33 B3 =WEEKDAY(A3)
34 C3 =WEEKDAY(A3)
35 D3 3
36 E3 1
37 F3 =IF(WEEKDAY(A3)>D3,A3-WEEKDAY(A3)+D3+E3*7,A3-WEEKDAY(A3)+D3+E3*7-7)
38 G3 =WEEKDAY(F3)
39 H3 =INT((DAY(F3)+6)/7)
40 I3  
41 J3 =IF(MONTH(IF(WEEKDAY(A3)>D3,A3-WEEKDAY(A3)+D3+E3*7,A3-WEEKDAY(A3)+D3+E3*7-7))<>MONTH(A3)," --",IF(WEEKDAY(A3)>D3,A3-WEEKDAY(A3)+D3+E3*7,A3-WEEKDAY(A3)+D3+E3*7-7) )

Previous Friday   (#previous_fri)

Previous Friday
D1: =B1 -CHOOSE(WEEKDAY(B1,1),2,3,4,5,6,7,1)
D1: =B1-MOD(B1,7)-1     (simpler)

Last Friday of Month
F1: =DATE(YEAR(B1),MONTH(B1)+1,0) -CHOOSE(WEEKDAY(DATE(YEAR(B1),MONTH(B1)+1,0),1),2,3,4,5,6,0,1)

Friday before Last Day of Month
H1: =DATE(YEAR(B1),MONTH(B1)+1,0) -CHOOSE(WEEKDAY(DATE(YEAR(B1),MONTH(B1)+1,0),1),2,3,4,5,6,7,1)

Modify formula for other previous days   (each formula on same row is equivalent)

 JKLMNOP
 1 2005-10-21 Fri   J1: =B$1-CHOOSE(WEEKDAY(B$1,1),2,3,4,5,6,7,1)   =B$1-(WEEKDAY(B$1+1))  =B$1-MOD(B$1+1,7)
 2 2005-10-20 Thu   J2: =B$1-CHOOSE(WEEKDAY(B$1,1),3,4,5,6,7,1,2)   =B$1-(WEEKDAY(B$1+2))  =B$1-MOD(B$1+2,7)
 3 2005-10-26 Wed   J3: =B$1-CHOOSE(WEEKDAY(B$1,1),4,5,6,7,1,2,3)   =B$1-(WEEKDAY(B$1+3))  =B$1-MOD(B$1+3,7)
 4 2005-10-25 Tue   J4: =B$1-CHOOSE(WEEKDAY(B$1,1),5,6,7,1,2,3,4)   =B$1-(WEEKDAY(B$1+4))  =B$1-MOD(B$1+4,7)
 5 2005-10-24 Mon   J5: =B$1-CHOOSE(WEEKDAY(B$1,1),6,7,1,2,3,4,5)   =B$1-(WEEKDAY(B$1+5))  =B$1-MOD(B$1+5,7)
 6 2005-10-23 Sun   J6: =B$1-CHOOSE(WEEKDAY(B$1,1),7,1,2,3,4,5,6)   =B$1-(WEEKDAY(B$1+6))  =B$1-MOD(B$1+6,7)
 7 2005-10-22 Sat   J7: =B$1-CHOOSE(WEEKDAY(B$1,1),1,2,3,4,5,6,7)   =B$1-(WEEKDAY(B$1))  =B$1-MOD(B$1,7)

More on Previuos Friday (#morefriday)

 BCDEFGHIJ
 1  Test Date   Prev Fri   Fri on or prev   Fri on or after   Fri after
 2  2005-10-27 Thu   2005-10-21 Fri   2005-10-21 Fri   2005-10-28 Fri   2005-10-28 Fri
 3  2005-10-28 Fri   2005-10-21 Fri   2005-10-28 Fri   2005-10-28 Fri   2005-11-04 Fri
 4  2005-10-29 Sat   2005-10-28 Fri   2005-10-28 Fri   2005-11-04 Fri   2005-11-04 Fri
 5  2005-10-30 Sun   2005-10-28 Fri   2005-10-28 Fri   2005-11-04 Fri   2005-11-04 Fri
 6  2005-10-31 Mon   2005-10-28 Fri   2005-10-28 Fri   2005-11-04 Fri   2005-11-04 Fri
 7  2005-11-01 Tue   2005-10-28 Fri   2005-10-28 Fri   2005-11-04 Fri   2005-11-04 Fri
 8  2005-11-02 Wed   2005-10-28 Fri   2005-10-28 Fri   2005-11-04 Fri   2005-11-04 Fri
 9  2005-11-03 Thu   2005-10-28 Fri   2005-10-28 Fri   2005-11-04 Fri   2005-11-04 Fri
10  2005-11-04 Fri   2005-10-28 Fri   2005-11-04 Fri   2005-11-04 Fri   2005-11-11 Fri
11  2005-11-05 Sat   2005-11-04 Fri   2005-11-04 Fri   2005-11-11 Fri   2005-11-11 Fri
12  2005-11-06 Sun   2005-11-04 Fri   2005-11-04 Fri   2005-11-11 Fri   2005-11-11 Fri
13  2005-11-07 Mon   2005-11-04 Fri   2005-11-04 Fri   2005-11-11 Fri   2005-11-11 Fri
14  2005-11-08 Tue   2005-11-04 Fri   2005-11-04 Fri   2005-11-11 Fri   2005-11-11 Fri
15  2005-11-09 Wed   2005-11-04 Fri   2005-11-04 Fri   2005-11-11 Fri   2005-11-11 Fri
16  2005-11-10 Thu   2005-11-04 Fri   2005-11-04 Fri   2005-11-11 Fri   2005-11-11 Fri
17  2005-11-11 Fri   2005-11-04 Fri   2005-11-11 Fri   2005-11-11 Fri   2005-11-18 Fri
18  2005-11-12 Sat   2005-11-11 Fri   2005-11-11 Fri   2005-11-18 Fri   2005-11-18 Fri
19  2005-11-13 Sun   2005-11-11 Fri   2005-11-11 Fri   2005-11-18 Fri   2005-11-18 Fri
20  2004-04-30 Fri   2004-04-23 Fri   2004-04-30 Fri   2004-04-30 Fri   2004-05-07 Fri
21          
22  D2: =B2-MOD(B2,7)-1
23  F2: =B2-MOD(B2,7)-1+7*(MOD(B2,7)=6)
24  H2: =B2-MOD(B2,7)+6
25  J2: =B2-MOD(B2,7)-1+7*(MOD(B2,7)=6)+7
26          
27  Test Date   Prev Mon   Mon on or prev   Mon on or after   Mon after
28  2005-10-27 Thu   2005-10-24 Mon   2005-10-24 Mon   2005-10-31 Mon   2005-10-31 Mon
29  2005-10-28 Fri   2005-10-24 Mon   2005-10-24 Mon   2005-10-31 Mon   2005-10-31 Mon
30  2005-10-29 Sat   2005-10-24 Mon   2005-10-24 Mon   2005-10-31 Mon   2005-10-31 Mon
31  2005-10-30 Sun   2005-10-24 Mon   2005-10-24 Mon   2005-10-31 Mon   2005-10-31 Mon
32  2005-10-31 Mon   2005-10-24 Mon   2005-10-31 Mon   2005-10-31 Mon   2005-11-07 Mon
33  2005-11-01 Tue   2005-10-31 Mon   2005-10-31 Mon   2005-11-07 Mon   2005-11-07 Mon
34  2005-11-02 Wed   2005-10-31 Mon   2005-10-31 Mon   2005-11-07 Mon   2005-11-07 Mon
35  2005-11-03 Thu   2005-10-31 Mon   2005-10-31 Mon   2005-11-07 Mon   2005-11-07 Mon
36  2005-11-04 Fri   2005-10-31 Mon   2005-10-31 Mon   2005-11-07 Mon   2005-11-07 Mon
37  2005-11-05 Sat   2005-10-31 Mon   2005-10-31 Mon   2005-11-07 Mon   2005-11-07 Mon
38  2005-11-06 Sun   2005-10-31 Mon   2005-10-31 Mon   2005-11-07 Mon   2005-11-07 Mon
39  2005-11-07 Mon   2005-10-31 Mon   2005-11-07 Mon   2005-11-07 Mon   2005-11-14 Mon
40  2005-11-08 Tue   2005-11-07 Mon   2005-11-07 Mon   2005-11-14 Mon   2005-11-14 Mon
41  2005-11-09 Wed   2005-11-07 Mon   2005-11-07 Mon   2005-11-14 Mon   2005-11-14 Mon
42  2005-11-10 Thu   2005-11-07 Mon   2005-11-07 Mon   2005-11-14 Mon   2005-11-14 Mon
43  2005-11-11 Fri   2005-11-07 Mon   2005-11-07 Mon   2005-11-14 Mon   2005-11-14 Mon
44  2005-11-12 Sat   2005-11-07 Mon   2005-11-07 Mon   2005-11-14 Mon   2005-11-14 Mon
45  2005-11-13 Sun   2005-11-07 Mon   2005-11-07 Mon   2005-11-14 Mon   2005-11-14 Mon
46  2004-04-30 Fri   2004-04-26 Mon   2004-04-26 Mon   2004-05-03 Mon   2004-05-03 Mon
47          
48  D28: =B28-MOD(B28,7)+2 -(MOD(B28,7)<3)*7
49  F28: =B28-MOD(MOD(B28,7)-2,7)
50  H28: =B28-MOD(B28,7)+2 -(MOD(B28,7)<3)*7+7
51  J28: =B28-MOD(MOD(B28,7)-2,7)+7

Related   (#related)


You are one of many distinguished visitors who have visited my site here or in a previous location  since opening on Aug 15, 1998.

Formatting for most of this page was accomplished with a program by Rob Bovey now at Application Professionals [Apps Pro], don't know which page you wouuld find it on: VBA Code Cleaner Dialog Converter VBA Code Documentor XY Chart Labeler,   Excel Utilities
Program is in Arhives at Baarns http://archive.baarns.com/EXCEL/PAGES/free.ASP
Of particular interest is the Excel to Web Table Converter (XL5 and up) which will convert selected cells to HTML and can include borders for empty cells.  Includes source code.  XL97 users especially will appreciate this one since MS dropped option choices in its converter (Save As).    If done today would use my own XL2HTML or to show row and column headers with the XL2HTMLx conversion.  See my page for more information on converting Excel to HTML and particularly in the Related area if not specifically covered in the main part of the page.

Return to David McRitchie's Excel Page. Return to Date & Time Page.  [Site Search -- Excel] 

Updated on: 1998-08-15, 2002-09-04.

Please send your comments concerning this web page to: David McRitchie send email comments


Copyright © 1997 - 2004,  F. David McRitchie,  All Rights Reserved