Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Getting date with last day of the month

Posted on 2007-04-07
3
312 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
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:…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

828 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