Solved

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

Posted on 2009-07-10
2
482 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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ā€¦
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that undeā€¦
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

726 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