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
Solved

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

Posted on 2009-07-10
2
481 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 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

856 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