.net - number of Saturdays and Sundays of a specific year and month -
using visual basic .net, how can find number of saturdays , sundays of specific year , month?
i have created function uses calculation method calculate sats , sundays. have not tested death. have tested performance of both , calculation method (below) , iteration method in @p.campbell's answer, result in milliseconds 10,000 calls were.
calculation: 7 iteration: 39
hope helps.
dave
dim month integer = 12 dim year integer = 2011 'calculate start , end of month dim current new datetime(year, month, 1) dim ending datetime = current.addmonths(1) 'ints hold results dim numsat integer = 0 dim numsun integer = 0 'numbers used in calculation dim datediff integer = (ending.date - current.date).days dim firstday dayofweek = current.dayofweek 'figure out how many whole weeks in month, there must sat , sunday in each ' note integer devision numsat = datediff / 7 numsun = datediff / 7 'calculate using day of week 1st , how many days on full weeks there ' note sunday requires bit sunday value 0 , saturday value 6 numsat += if((firstday + (datediff mod 7)) > (dayofweek.saturday), 1, 0) numsun += if(((firstday + (datediff mod 7)) > (dayofweek.saturday + 1)) or (firstday = dayofweek.sunday , (datediff mod 7 = 1)), 1, 0) 'output results console.writeline("sats: " & numsat) console.writeline("suns: " & numsun) console.readline()
Comments
Post a Comment