locked
Last Day of The Current Month RRS feed

  • Question

  • User952204704 posted
    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.
    Wednesday, December 31, 2003 6:34 PM

All replies

  • User1806792307 posted
    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.
    Friday, January 2, 2004 5:14 PM
  • User952204704 posted
    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.
    Thursday, January 15, 2004 3:48 PM
  • User1502633704 posted
    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.
    Tuesday, January 27, 2004 12:54 PM
  • User1806792307 posted
    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)
    Tuesday, January 27, 2004 1:15 PM
  • User-1896916166 posted

    For a last day of month from ANY date given:

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

     

     

    Thursday, September 10, 2009 10:56 AM