How to get the last date of the month?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi, I have a datetime variable. How can I get the date for the end of the
month according to that varilable's month and year?

Thanks,
Alpha
 
DateTime dt = DateTime.Now;

DateTime firstDayOfThisMonth = dt.Subtract(TimeSpan.FromDays(dt.Day - 1));

Console.WriteLine("First Day of This Month:" + firstDayOfThisMonth);

DateTime firstDayOfNextMonth = firstDayOfThisMonth.AddMonths(1);

Console.WriteLine("First Day of next month" +
firstDayOfNextMonth.ToString());

DateTime lastDayOfThisMonth =
firstDayOfNextMonth.Subtract(TimeSpan.FromDays(1));

Console.WriteLine("Last day of this month:"+lastDayOfThisMonth.ToString());
 
Hi, I have a datetime variable. How can I get the date for the end of the
month according to that varilable's month and year?


private DateTime LastDayOfMonth(DateTime pdtmDate)
{
DateTime dtmDate = new DateTime(pdtmDate.Year, pdtmDate.Month, 1);
return dtmDate.AddMonths(1).AddDays(-1);
}
 
Alpha said:
Hi, I have a datetime variable. How can I get the date for the end of the
month according to that varilable's month and year?

Thanks,
Alpha
DateTime d = new DateTime(Year, Month, DateTime.DaysInMonth(Year, Month));
 
Back
Top