List the First Monday of every month using C# and VB.NET

Here’s some code for listing down the first Monday of every month in an year using C# and VB.NET. The Monday’s listed in this example are for the year 2010

C#

public static void Main(string[] args)
{
for (int mth = 1; mth <= 12; mth++)
{
DateTime dt = new DateTime(2010, mth, 1);
while (dt.DayOfWeek != DayOfWeek.Monday)
{
dt = dt.AddDays(1);
}
Console.WriteLine(dt.ToLongDateString());
}
Console.ReadLine();
}

VB.NET

Public Shared Sub Main(ByVal args() As String)
For mth As Integer = 1 To 12
Dim dt As New DateTime(2010, mth, 1)
Do While dt.DayOfWeek <> DayOfWeek.Monday
dt = dt.AddDays(1)
Loop
Console.WriteLine(dt.ToLongDateString())
Next mth
Console.ReadLine()
End Sub

OUTPUT

image



Will you give this article a +1 ? Thanks in advance


About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

1 comment:

Vijay kumar said...

Hi, This is Vijay here.

I need a VB.net function that finds out the last Thursday of the month, and adds 1 more day to it.

I have similar function that reads the 3rd Friday of the month & adds 1 more day to it.
-----------------

Function changeexpiration(ByVal dt As Date, ByVal num As Integer) As Date

'changes the date supplied to the 15th, adds or subtracts num month(s) to the date,
'and finds the first Friday on or after the 15th, which must be the 3rd Friday of that month

dt = dt.AddDays(15 - dt.Day)
dt = dt.AddMonths(num)

For x As Integer = 1 To 7
If dt.DayOfWeek = DayOfWeek.Friday Then
Exit For
End If
dt = dt.AddDays(1)
Next

Return dt.AddDays(1) 'actual expiration is the Saturday after close of trade on expiration Friday

End Function
---------------------

Thanks & regds.,

Vijay