Loading ...

C#: How to get the end of the day | CodeAsp.Net

C#: How to get the end of the day

 /5
0 (0votes)

Yeterday I had a requirement where I needed to find the end of day in datetime. I decided to make extension on datetime to achieve it. Below is the sample code:
DateTimeExtension.cs:

#region Using Directives

using System;

#endregion

namespace Sample.Console
{
    public static class DateTimeExtension
    {
        public static DateTime GetEndOfDay(this DateTime datetime)
        {
            return datetime.AddDays(1).AddTicks(-1);
        }
    }//end class
}//end namespace

Sample code to test it:
using System;
using System.Text.RegularExpressions;

namespace Sample.Console
{
    internal class Program
    {
        private static void Main(string[] args)
        {

            DateTime t = DateTime.Today.GetEndOfDay();

            System.Console.WriteLine(t);
            System.Console.ReadLine();
        }

       
    }
}

Output:

Comments (no comments yet)

Top Posts