Solved

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

Posted on 2009-07-10
2
477 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
2 Comments
 
LVL 142

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This video teaches viewers about errors in exception handling.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

747 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

10 Experts available now in Live!

Get 1:1 Help Now