Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,

I want to find the first and last date of a month. can anyone help me out how to get it. I have a combobox filled with months i.e. January, February,March,...... and so on. Now what i want is i will select the Month from the combobox, then i should get the first and last date of that month. Can any one help me out in doing so.



Thanks in Advance

Jagdish
Posted

I found this[^] in 1st page of Google[^]. Always hit in search engine before asking here because it's frequent & easy one, You can find this by searching in CP.
 
Share this answer
 
Comments
Jagdish.rkl 6-Dec-10 0:21am    
Thanks a lot. i got it.
Well quite obviously the first day of the month is 1 :rolleyes:

The last day can be obtained from Calendar.GetDaysInMonth[^]
 
Share this answer
 
Comments
Jagdish.rkl 6-Dec-10 0:21am    
Thanks a lot. i got it.
Private Function GetFirstDateOfMonth(ByVal iMonth As Integer)<br />
        Dim dtFrom As DateTime = New DateTime(DateTime.Now.Year, iMonth, 1)<br />
        dtFrom = dtFrom.AddDays(-(dtFrom.Day - 1))<br />
        Return dtFrom<br />
    End Function<br />
<br />
    Private Function GetLastDateOfMonth(ByVal iMonth As Integer)<br />
        Dim dtTo As New DateTime(DateTime.Now.Year, iMonth, 1)<br />
        dtTo = dtTo.AddMonths(1)<br />
        dtTo = dtTo.AddDays(-(dtTo.Day))<br />
        Return dtTo<br />
    End Function



I found the answer for my query so thought of posting it here. we can get the first and last date of a month from these functions.
 
Share this answer
 
Comments
[no name] 6-Dec-10 8:56am    
The first day of the month is always 1, you don't need a method to return 1 !!
Did you even look at the response that was given? This is completely unnecessary and incorrect!!!

Why ask for help if you are simply going to ignore it?
To get first and last day of month you can use a little trick with DateSerial() function, like this:
VB
Sub Main()
    Dim fDayOfMonth As Date = Nothing
    Dim lDayOfMonth As Date = Nothing
    Dim i As Integer = 0
    For i = 1 To 12
        fDayOfMonth = GetFirstDayOfMonth(2010, i)
        lDayOfMonth = GetLastDayOfMonth(2010, i)
        Console.WriteLine("first day: " & fDayOfMonth & "; last day: " & lDayOfMonth)
    Next
    Console.ReadLine()
End Sub
Function GetFirstDayOfMonth(ByVal _year As Integer, ByVal _month As Integer) As Date
    Return DateSerial(_year, _month, 1)
End Function
Function GetLastDayOfMonth(ByVal _year As Integer, ByVal _month As Integer) As Date
    Return DateSerial(_year, _month + 1, 1 - 1)
End Function

This should always return good value (date), no matter how many days (28/29) has February.
 
Share this answer
 
Comments
Jagdish.rkl 7-Dec-10 0:09am    
Thanks a lot
[no name] 8-Dec-10 12:21pm    
There is absolutely unnecessary. There is already a method in .NET to accomplish this.
Maciej Los 8-Dec-10 15:44pm    
Mark, i know it's not "clear" VB.NET method, but you can add reference to Visual Basic Runtime Library (in Microsoft.VisualBasic.dll) and use it as any others .net functions.
-----------------------------------
VB.NET still supports 'DateSerial'.

--
M S Herfried K. Wagner
M V P <url:http: dotnet.mvps.org="">
--------------------------------------

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900