tag:blogger.com,1999:blog-6497434045878013201.post5279897610922577350..comments2023-12-06T18:45:51.800-08:00Comments on Dot Net Tips & Tricks , C# (C Sharp)Tips & Tricks: How to get number of Sundays/Saturdays in a given month using (C#/VB.NET)Unknownnoreply@blogger.comBlogger1125tag:blogger.com,1999:blog-6497434045878013201.post-54233907520646699622012-04-04T01:07:59.637-07:002012-04-04T01:07:59.637-07:00The number of Saturdays or Sundays will always be ...The number of Saturdays or Sundays will always be 4 or 5. After much gnashing of teeth, here is the single formula that gives you the answer.<br /><br />iif(Today.AddDays(-Today.Day).Subtract(Today.AddDays(-Today.Day).AddMonths(-1).AddDays(4-Today.AddDays(-Today.Day).AddMonths(-1).DayOfWeek)).TotalDays/7 > 4,5,4)<br /><br />1. Today.AddDays(-Today.Day) gives you the last day of the previous month.<br /><br />2. Today.AddDays(-Today.Day).AddMonths(-1) gives you the last day of the month before that.<br /><br />3. AddDays(4-Today.AddDays(-Today.Day).AddMonths(-1).DayOfWeek)) in this case I'm looking for the first thursday of the month so I subtract the last day of the previous month's day of the week from 4. If we were looking for the first sunday I would subtract last day of the previous month from 7. In any case, I end up with the first Monday-Sunday of the month.<br /><br />4. Subtract is a timespan function that gives me the time elapsed between the two dates. The TotalDays method converts this time span to days.<br /><br />5. Dividing the total days elapsed between the first ___day and 7 gives me the number of weeks between the two dates. If that number is greater than 4 then I know I started a 5th week beginning on that day.Mike Hansenhttps://www.blogger.com/profile/01684205838102639138noreply@blogger.com