Solved

Getting date with last day of the month

Posted on 2007-04-07
3
310 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
ID: 18869137
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
ID: 18870493
*** 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
ID: 18874918
try this
DateTime dt1 = DateTime.Now;
Response.Write( (dt1.AddDays(-(DateTime.Now.Day -1 ))).AddMonths(1).AddDays(-1));
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

929 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

11 Experts available now in Live!

Get 1:1 Help Now