AspBucket offers ASP.NET, C#, VB, Jquery, CSS, Ajax, SQL tutorials. It is the best place for programmers to learn

Monday 1 February 2016

Some common date operations in c#

Here I am sharing some common date functions like get First day of Month, last day of month, start time of day, end time of day etc.  Please check below code & add to Helper class of project. I hope it may helpful for you.

Code:


       #region--First Day Of Month--
        public DateTime FirstDayOfMonth(DateTime dateTime)
        {
            return new DateTime(dateTime.Year, dateTime.Month, 1);
        }
        #endregion

        #region--Last Day Of Month--
        public DateTime LastDayOfMonth(DateTime dateTime)
        {
            dateTime = new DateTime(dateTime.Year, dateTime.Month, 1).AddMonths(1);
            return dateTime.AddDays(-1);
        }
        #endregion

        #region--Get Start Time of Day--
        public DateTime GetStartTimeOfDay(DateTime dateTime)
        {
            return new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, 0, 0, 0, 0);
        }
        #endregion

        #region--Get End Time of Day--
        public DateTime GetEndTimeOfDay(DateTime dateTime)
        {
            return dateTime.AddDays(1).Date.AddSeconds(-1);
        }
        #endregion

0 comments :

Post a Comment

  • Popular Posts
  • Comments