Getting End Date of Last Month in C#
In this article, I would like to show the End Date of Last Month with Now property in C#.
In this article, I would like to show the End Date of Last Month with Now property in C#. To do that you can use month property with datetime now and today property.
DateTime Now Property
This displays the current date and time of the system, expressed as the local time. In other words the Now property returns a DateTime object that has the date and time values for right now.
Month Property
This property is used to get month component of the date.
End Date of Last Month in C#
You can do it in C#. The following is simple code for it.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication71
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Today: {0}", DateTime.Now);
var today = DateTime.Now;
var month = new DateTime(today.Year, today.Month, 1);
var last = month.AddDays(-1);
Console.WriteLine(" End Date of Last Month : {0}", last.ToString("yyy/MM/dd"));
}
}
}
Output