C# Sharp Exercises: Find the last day of a month against a given date
C# Sharp DateTime: Exercise-55 with Solution
Write a program in C# Sharp to find the last day of a month against a given date.
Sample Solution:-
C# Sharp Code:
using System;
class dttimeex55
{
static void Main()
{
int yr,mn,dt;
Console.Write("\n\n Find the last day of the month against a given date :\n");
Console.Write("----------------------------------------------------------\n");
Console.Write(" Input the Day : ");
dt = Convert.ToInt32(Console.ReadLine());
Console.Write(" Input the Month : ");
mn = Convert.ToInt32(Console.ReadLine());
Console.Write(" Input the Year : ");
yr = Convert.ToInt32(Console.ReadLine());
DateTime d = new DateTime(yr, mn, dt);
Console.WriteLine(" The formatted Date is : {0}",d.ToString("dd/MM/yyyy"));
Console.WriteLine(" The last day of the month for the above date is : {0}\n", LastDayOfMonth(d).ToString("dd/MM/yyyy"));
}
public static DateTime LastDayOfMonth(DateTime dt)
{
DateTime ss= new DateTime(dt.Year, dt.Month, 1);
return ss.AddMonths(1).AddDays(-1);
}
}
Sample Output:
Find the last day of the month against a given date : ---------------------------------------------------------- Input the Day : 10 Input the Month : 02 Input the Year : 2017 The formatted Date is : 10/02/2017 The last day of the month for the above date is : 28/02/2017
Flowchart:

C# Sharp Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Write a program in C# Sharp to find the first day of the month against a given date.
Next: Write a program in C# Sharp to find the First day of next month against a given date.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Exercises: Weekly Top 12 Most Popular Topics
- Pandas DataFrame: Exercises, Practice, Solution
- Conversion Tools
- JavaScript: HTML Form Validation
- SQL Exercises, Practice, Solution - SUBQUERIES
- C Programming Exercises, Practice, Solution : For Loop
- Python Exercises, Practice, Solution
- Python Data Type: List - Exercises, Practice, Solution
- C++ Basic: Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - exercises on Employee Database
- SQL Exercises, Practice, Solution - exercises on Movie Database
- SQL Exercises, Practice, Solution - exercises on Soccer Database
- C Programming Exercises, Practice, Solution : Recursion