;

How to find the First and Last day of the month of a given date using C#


Tutorialsrack 20/10/2019 C#

In this article, we will learn how to find the first and last day of the month of a given date using c#.

In .NET Framework, a DateTime class has no property or method to find the first and last day of the month using a specific date. So we are going to use this trick to find the first and last day of the month.

Example -  To find the first and last day of the month
using System;

namespace Tutorialsrack
{
    class Program
    {
        /*Find the First and Last Day Of the Month using C#*/
        static void Main(string[] args)
        {
            //initialize a datetime variable with today
            DateTime date = DateTime.Now;

            //create a datetime object with first day of current month
            DateTime FirstDay_of_Month = new DateTime(date.Year,date.Month,1);

            //Find the Last day of current month
            DateTime LastDay_of_Month = FirstDay_of_Month.AddMonths(1).AddSeconds(-1);

            Console.WriteLine("First Day of The Month is: {0}", FirstDay_of_Month);
            Console.WriteLine("Last Day of The Month is: {0}", LastDay_of_Month);

            Console.ReadKey();
        }
    }
}
Output

First Day of The Month is: 01-10-2019 00:00:00
Last Day of The Month is: 31-10-2019 23:59:59

I hope this article will help you to understand how to find the First and Last day of the month of a given date using C#.

Share your valuable feedback, please post your comment at the bottom of this article. Thank you!


Related Posts



Comments

Recent Posts
Tags