Finding Last day of Month

Posted on 2006-04-27
Last Modified: 2011-09-20
How do i find the last day of a month. I need to find the first and last date of a previous month.

Todays date : 04/25/2006

Result : 03/01/2006 - 03/31/2006

Any ideas ?

Question by:karthikram
    LVL 69

    Assisted Solution

    by:Éric Moreau
    Hi karthikram,

    I use these methods:
        Public Shared Function GetBeginOfCurrentMonth(ByVal pDate As Date) As Date
            Return Date.Parse(pDate.Year.ToString & "/" & pDate.Month.ToString & "/01")
        End Function
        Public Shared Function GetBeginOfPreviousMonth(ByVal pDate As Date) As Date
            Dim dtmEOPM As Date = GetEndOfPreviousMonth(pDate)
            Return Date.Parse(dtmEOPM.Year.ToString & "/" & dtmEOPM.Month.ToString & "/01")
        End Function
        Public Shared Function GetEndOfPreviousMonth(ByVal pDate As Date) As Date
            Return GetBeginOfCurrentMonth(pDate).AddDays(-1)
        End Function

    LVL 7

    Accepted Solution

    This will do it:

        Public Function LastDayOfMonth(ByVal p_Month As Integer) As DateTime
            Dim dtm As New DateTime(Now.Year, p_Month + 1, 1)
            Return dtm.AddDays(-1)
        End Function

    Dim lastDay as DateTime = LastDayOfMonth(3) 'last day of march

    Featured Post

    Live: Real-Time Solutions, Start Here

    Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

    Join & Write a Comment

    IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    754 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

    16 Experts available now in Live!

    Get 1:1 Help Now