Solved

Getting date with last day of the month

Posted on 2007-04-07
3
313 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
[X]
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
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 86

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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:…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 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