Solved

C# -- "Last Day of the Month" Calculation ?

Posted on 2009-07-10
2
483 Views
Last Modified: 2012-05-07
Revise below to get last day of
this month instead of TODAY.
--------------------------------------------
      private void button1_Click(object sender, EventArgs e)
      {
          string today = String.Format("Last Day of this month is {0}", DateTime.Today.ToString("D"));
          MessageBox.Show(today);
      }
0
Comment
Question by:finance_teacher
[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
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 24827691
DateTime lastDayOfThisMonth = new DateTime(today.Year, today.Month, 1).AddMonths(1).AddDays(-1);
0
 
LVL 41

Assisted Solution

by:guru_sami
guru_sami earned 250 total points
ID: 24827980
angelIII 's code should work perfectly if you are looking for lastDate of the month..
But if you are looking for last day i.e. mon/tue/wed etc...you should add one more line to that code:

DateTime lastDayOfThisMonth = new DateTime(today.Year, today.Month, 1).AddMonths(1).AddDays(-1);
string lastday = lastDayOfThisMonth.DayOfWeek.ToString();

Or here is some more lines of code:

DateTime today = DateTime.Today;
int daysinmonth = DateTime.DaysInMonth(today.Year, today.Month);
int day = today.Day;
DateTime lastDate =today.AddDays(daysinmonth - day);
string lastDay = lastDate.DayOfWeek.ToString();
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

695 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