Last Day of The Current Month

Rate It (1)

Last post 09-10-2009 10:56 AM by alejjak. 5 replies.

Sort Posts:

  • Last Day of The Current Month

    12-31-2003, 6:34 PM
    • Participant
      1,260 point Participant
    • Intrino
    • Member since 05-19-2003, 4:28 PM
    • Posts 252
    Hi,

    This is something I was looking for a few hours and found it here

    4guysfromrolla site is really helpful but I think this function should be in FAQ as well.

    To get the last day of the month their routine found the last day of the last month.

    To find the last day of the current month instead of last month, just add 1 to the month portion:

    DateSerial(Year(CurrentDate), Month(CurrentDate) + 1, 1 - 1)

    As was explained on the site, you can similarly find out what the last day of a month 2 years ago, by subtracting - 2 for the year part:

    DateSerial(Year(CurrentDate) - 2, Month(CurrentDate) + 1, 1 - 1)

    Hope this helps out. But visit 4GuysFromRolla for more articles on various other things.


    .intrino.
  • Re: Last Day of The Current Month

    01-02-2004, 5:14 PM
    • All-Star
      30,305 point All-Star
    • PLBlum
    • Member since 06-28-2002, 1:20 PM
    • Boston, MA
    • Posts 5,344
    • TrustedFriends-MVPs
    If you use the .net DateTime structure, you can get the last day of the month this way:

    new DateTime(currentyear, currentmonth, 1).AddMonth(1).AddDay(-1)

    Personally, I recommend that VB.NET users investigate the DateTime structure as it is the standard tool for all .net languages and contains a very rich set of properties and methods.
    --- Peter Blum
    Creator of Peter's Data Entry Suite (formerly Professional Validation And More and Peter's Date Package) and Peter's Polling Package
    www.PeterBlum.com
  • Re: Last Day of The Current Month

    01-15-2004, 3:48 PM
    • Participant
      1,260 point Participant
    • Intrino
    • Member since 05-19-2003, 4:28 PM
    • Posts 252
    I agree with PLBlum,

    I wasn't able to find any references to that. Must have not been looking hard enough or in the right places.

    But good solution.

    .intrino.
  • Re: Last Day of The Current Month

    01-27-2004, 12:54 PM
    • Member
      660 point Member
    • hotblue
    • Member since 10-14-2002, 9:56 AM
    • Makati City, Philippines
    • Posts 132
    DateTime lastDay = DateTime.Today.AddMonths(1).AddDays(-1);

    This should work for the current day, but what if you have a Date AND Time (e.g. 01/27/2004 5:32AM) and want to get the last day of that month? Here's my cheat:

    private DateTime LastDayOfMonth(DateTime theDay) {
    
    return DateTime.Parse(String.Format("{0:MM/dd/yyyy}",theDay)).AddMonths(1).AddDays(-1);
    }

    Hope that helps ;)

    P.S. if you can't find any references to a particular problem, try http://www.hotblue.com, a new .NET directory which has indexed over 8,000+ articles from hundreds of .NET sites.
    Willeus Acuña :)

    Visit hotblue.com - tons of advice on .NET programming!

    Comprehensive .NET directory with 8,000+ articles indexed.
  • Re: Last Day of The Current Month

    01-27-2004, 1:15 PM
    • All-Star
      30,305 point All-Star
    • PLBlum
    • Member since 06-28-2002, 1:20 PM
    • Boston, MA
    • Posts 5,344
    • TrustedFriends-MVPs
    Simply in terms of efficiency, its far quicker to create a new DateTime object from numerical values than to convert to a string then back again. So for the example where your DateTime contains 1/27/2004 at 5:32AM:

    new DateTime(origDT.Year, origDT.Month, origDT.Day).AddMonths(1).AddDays(-1)
    --- Peter Blum
    Creator of Peter's Data Entry Suite (formerly Professional Validation And More and Peter's Date Package) and Peter's Polling Package
    www.PeterBlum.com
  • Re: Last Day of The Current Month

    09-10-2009, 10:56 AM
    • Member
      12 point Member
    • alejjak
    • Member since 06-03-2009, 5:43 PM
    • Florida
    • Posts 7

    For a last day of month from ANY date given:

     new DateTime(origDt.Year, origDT.Month, 1).AddMonths(1).AddDays(-1)

     

     

    Filed under:
Page 1 of 1 (6 items)