Solved

Getting date with last day of the month

Posted on 2007-04-07
3
309 Views
Last Modified: 2013-11-26
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
0
Comment
Question by:vj_mi
3 Comments
 
LVL 7

Accepted Solution

by:
TelnetServices earned 500 total points
Comment Utility
Easy... use DateTime.DaysInMonth....

    Private Function LastDayOfMonth(ByVal myDate As Date) As Date
        Dim myDay As Integer
        Dim myMonth As Integer = Month(myDate)
        Dim myYear As Integer = Year(myDate)
        myDay = DateTime.DaysInMonth(myYear, myMonth)
        Dim myDateTime As New Date(myYear, myMonth, myDay)
        Return myDateTime
    End Function

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
*** 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
0
 
LVL 6

Expert Comment

by:dash420
Comment Utility
try this
DateTime dt1 = DateTime.Now;
Response.Write( (dt1.AddDays(-(DateTime.Now.Day -1 ))).AddMonths(1).AddDays(-1));
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now