Getting Start Date of Last Month in C#

In this article, I would like to show how to get Start Date of Last Month with Now property in C#.
  • 4068

In this article, I would like to show the Start 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.

Start 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 first = month.AddMonths(-1);

            Console.WriteLine("Start Date of Last Month : {0}", first.ToString("yyy/MM/dd"));  

        }

    }
}


Output


Image1.jpg

 

© 2018 DotNetHeaven. All rights reserved.