Link to home
Start Free TrialLog in
Avatar of vj_mi
vj_mi

asked on

Getting date with last day of the month

The user enters date. Now from that I need to modify the date entered in such a way that I get the date with the last day of the month. For example, if user enters 12/12/2007, then I want the code to modify this value to 31/12/2007. Or if it is 12/2/xxxx, then I should get 28/2/xxxx or 29/2/xxxx. How can I do that?

Regards,
MI
ASKER CERTIFIED SOLUTION
Avatar of TelnetServices
TelnetServices
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mike Tomlinson
*** Not for Points ***

You can use Date.Year() and Date.Month() instead of Microsoft.VisualBasic.Month() and Microsoft.VisualBasic.Year().

Re-written as a "one liner":

    Private Function LastDayOfMonth(ByVal myDate As Date) As Date
        Return New Date(myDate.Year, myDate.Month, DateTime.DaysInMonth(myDate.Year, myDate.Month))
    End Function

This is another common way it is done:

    Private Function LastDayOfMonth(ByVal myDate As Date) As Date
        Return New Date(myDate.Year, myDate.Month, 1).AddMonths(1).AddDays(-1)
    End Function
try this
DateTime dt1 = DateTime.Now;
Response.Write( (dt1.AddDays(-(DateTime.Now.Day -1 ))).AddMonths(1).AddDays(-1));