Wednesday, April 7, 2010

Get date for specified day of month in asp.net

'This code can be used when you want to find any specific date based on day of month
'like first monday of month,second tues day of month etc.


'declare first day of month
Dim M As String = "9"
Dim Y As String = "2011"
Dim fdm As DateTime = Convert.ToDateTime(M & "/01/" & Y)
'if suppose you have to find second friday of a month then use
fdm = fdm.AddDays(7) 'so that you will get a date of second week
'then loop to find the day specified as shown below
While Not fdm.DayOfWeek.ToString().ToLower().Contains("friday")
fdm = fdm.AddDays(1) 'move forward one day untill u get the specified Day
End While

lblDate.Text = fdm.ToString()

'similarly add 11 days for 3rd day, 21 days for 4th day.
'for first day do not add any day