Click here to Skip to main content
12,302,493 members (65,062 online)
Click here to Skip to main content
Add your own
alternative version

Stats

271.2K views
1.7K downloads
99 bookmarked
Posted

C# DateTime Library

, 19 Dec 2005
Rate this:
Please Sign up or sign in to vote.
Common DateTime routines for C#.

Introduction

I recently made the transition from Delphi to C#. I noticed that some of the datetime utilities that I used to use were not available in C#. So I recreated them. Hopefully this will save someone some time in the future.

The Code

using System;
namespace Concepts2Code.Utilities
{
    /// <summary>
    /// Common DateTime Methods.
    /// </summary>
    /// 

    public enum Quarter
    {
        First = 1,
        Second = 2,
        Third = 3,
        Fourth = 4
    }

    public enum Month
    {
        January = 1,
        February = 2,
        March = 3,
        April = 4,
        May = 5,
        June = 6,
        July = 7,
        August = 8,
        September = 9,
        October = 10,
        November = 11,
        December = 12
    }

    public class DateUtilities
    {
        #region Quarters

        public static DateTime GetStartOfQuarter( int Year, Quarter Qtr )
        {
            if( Qtr == Quarter.First )    // 1st Quarter = January 1 to March 31
                return new DateTime( Year, 1, 1, 0, 0, 0, 0 );
            else if( Qtr == Quarter.Second ) // 2nd Quarter = April 1 to June 30
                return new DateTime( Year, 4, 1, 0, 0, 0, 0 );
            else if( Qtr == Quarter.Third ) // 3rd Quarter = July 1 to September 30
                return new DateTime( Year, 7, 1, 0, 0, 0, 0 );
            else // 4th Quarter = October 1 to December 31
                return new DateTime( Year, 10, 1, 0, 0, 0, 0 );
        }

        public static DateTime GetEndOfQuarter( int Year, Quarter Qtr )
        {
            if( Qtr == Quarter.First )    // 1st Quarter = January 1 to March 31
                return new DateTime( Year, 3, 
                       DateTime.DaysInMonth( Year, 3 ), 23, 59, 59, 999 );
            else if( Qtr == Quarter.Second ) // 2nd Quarter = April 1 to June 30
                return new DateTime( Year, 6, 
                       DateTime.DaysInMonth( Year, 6 ), 23, 59, 59, 999 );
            else if( Qtr == Quarter.Third ) // 3rd Quarter = July 1 to September 30
                return new DateTime( Year, 9, 
                       DateTime.DaysInMonth( Year, 9 ), 23, 59, 59, 999 );
            else // 4th Quarter = October 1 to December 31
                return new DateTime( Year, 12, 
                       DateTime.DaysInMonth( Year, 12 ), 23, 59, 59, 999 );
        }

        public static Quarter GetQuarter( Month Month )
        {
            if( Month <= Month.March )
            // 1st Quarter = January 1 to March 31
                return Quarter.First;
            else if( ( Month >= Month.April ) && ( Month <= Month.June ) )
            // 2nd Quarter = April 1 to June 30
                return Quarter.Second;
            else if( ( Month >= Month.July ) && ( Month <= Month.September ) )
            // 3rd Quarter = July 1 to September 30
                return Quarter.Third;
            else // 4th Quarter = October 1 to December 31
                return Quarter.Fourth;
        }

        public static DateTime GetEndOfLastQuarter()
        {                 
            if( (Month)DateTime.Now.Month <= Month.March )
            //go to last quarter of previous year
                return GetEndOfQuarter( DateTime.Now.Year - 1, Quarter.Fourth);
            else //return last quarter of current year
                return GetEndOfQuarter( DateTime.Now.Year, 
                  GetQuarter( (Month)DateTime.Now.Month));
        }

        public static DateTime GetStartOfLastQuarter()
        {
            if( (Month)DateTime.Now.Month <= Month.March )
            //go to last quarter of previous year
                return GetStartOfQuarter( DateTime.Now.Year - 1, Quarter.Fourth);
            else //return last quarter of current year
                return GetStartOfQuarter( DateTime.Now.Year, 
                  GetQuarter( (Month)DateTime.Now.Month));
        }

        public static DateTime GetStartOfCurrentQuarter()
        {
            return GetStartOfQuarter( DateTime.Now.Year, 
                   GetQuarter( (Month)DateTime.Now.Month ));
        }

        public static DateTime GetEndOfCurrentQuarter()
        {
            return GetEndOfQuarter( DateTime.Now.Year, 
                   GetQuarter( (Month)DateTime.Now.Month ));
        }
        #endregion

        #region Weeks
        public static DateTime GetStartOfLastWeek()
        {
            int DaysToSubtract = (int)DateTime.Now.DayOfWeek + 7;
            DateTime dt = 
              DateTime.Now.Subtract(System.TimeSpan.FromDays( DaysToSubtract ) );
            return new DateTime( dt.Year, dt.Month, dt.Day, 0, 0, 0, 0 );
        }

        public static DateTime GetEndOfLastWeek()
        {
            DateTime dt = GetStartOfLastWeek().AddDays(6);
            return new DateTime( dt.Year, dt.Month, dt.Day, 23, 59, 59, 999 );
        }

        public static DateTime GetStartOfCurrentWeek()
        {
            int DaysToSubtract = (int)DateTime.Now.DayOfWeek ;
            DateTime dt = 
              DateTime.Now.Subtract( System.TimeSpan.FromDays( DaysToSubtract ) );
            return new DateTime( dt.Year, dt.Month, dt.Day, 0, 0, 0, 0 );
        }

        public static DateTime GetEndOfCurrentWeek()
        {
            DateTime dt = GetStartOfCurrentWeek().AddDays(6);
            return new DateTime( dt.Year, dt.Month, dt.Day, 23, 59, 59, 999 );
        }
        #endregion

        #region Months

        public static DateTime GetStartOfMonth( Month Month, int Year )
        {
             return new DateTime( Year, (int)Month, 1, 0, 0, 0, 0 );
        }

        public static DateTime GetEndOfMonth( Month Month, int Year )
        {
            return new DateTime( Year, (int)Month, 
               DateTime.DaysInMonth( Year, (int)Month ), 23, 59, 59, 999 );
        }

        public static DateTime GetStartOfLastMonth()
        {
            if( DateTime.Now.Month == 1 )
                return GetStartOfMonth( 12, DateTime.Now.Year - 1);
            else
                return GetStartOfMonth( DateTime.Now.Month -1, DateTime.Now.Year );
        }

        public static DateTime GetEndOfLastMonth()
        {
            if( DateTime.Now.Month == 1 )
                return GetEndOfMonth( 12, DateTime.Now.Year - 1);
            else
                return GetEndOfMonth( DateTime.Now.Month -1, DateTime.Now.Year );
        }

        public static DateTime GetStartOfCurrentMonth()
        {
            return GetStartOfMonth( DateTime.Now.Month, DateTime.Now.Year );
        }

        public static DateTime GetEndOfCurrentMonth()
        {
            return GetEndOfMonth( DateTime.Now.Month, DateTime.Now.Year );
        }
        #endregion

        #region Years
        public static DateTime GetStartOfYear( int Year )
        {
            return new DateTime( Year, 1, 1, 0, 0, 0, 0 );
        }

        public static DateTime GetEndOfYear( int Year )
        {
            return new DateTime( Year, 12, 
              DateTime.DaysInMonth( Year, 12 ), 23, 59, 59, 999 );
        }

        public static DateTime GetStartOfLastYear()
        {
            return GetStartOfYear( DateTime.Now.Year - 1 );
        }

        public static DateTime GetEndOfLastYear()
        {
            return GetEndOfYear( DateTime.Now.Year - 1 );
        }

        public static DateTime GetStartOfCurrentYear()
        {
            return GetStartOfYear( DateTime.Now.Year );
        }

        public static DateTime GetEndOfCurrentYear()
        {
            return GetEndOfYear( DateTime.Now.Year );
        }
        #endregion        

        #region Days
        public static DateTime GetStartOfDay( DateTime date )
        {
            return new DateTime( date.Year, date.Month, date.Day, 0, 0, 0, 0 );
        }

        public static DateTime GetEndOfDay( DateTime date )
        {
            return new DateTime( date.Year, date.Month, 
                                 date.Day, 23, 59, 59, 999 );
        }
        #endregion
      }
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Michael Ceranski
Software Developer (Senior) Concepts2Code
United States United States
Michael is the co-founder and master consultant for Concepts2Code, a software consulting company based in Buffalo, New York. He's been programming since the early 1990's. His vast programming experience includes VB, Delphi, C#, ASP, ASP.NET, Ruby on Rails, Coldfusion and PHP. Michael also is a Microsoft Certified Application Developer and a Certified Technology Specialist for SQL Server.

Visit his blog.

You may also be interested in...

Comments and Discussions

 
GeneralReason for vote 4 Pin
bilal7869913-Feb-13 7:45
memberbilal7869913-Feb-13 7:45 
QuestionThis is article was very helpful Pin
Espiritu8-Apr-12 18:44
memberEspiritu8-Apr-12 18:44 
GeneralMy vote of 5 Pin
Espiritu8-Apr-12 18:43
memberEspiritu8-Apr-12 18:43 
QuestionNice, good job Pin
cruzanPC2-Dec-11 5:30
membercruzanPC2-Dec-11 5:30 
Generalfirst day of week monday (International ) Pin
mindweaver17-Jan-11 3:21
membermindweaver17-Jan-11 3:21 
GeneralRe: first day of week monday (International ) Pin
Jani Giannoudis19-Mar-11 21:09
memberJani Giannoudis19-Mar-11 21:09 
Generalthank you very much. Pin
Howdy Nun20-May-09 11:47
memberHowdy Nun20-May-09 11:47 
GeneralGetEndOfLastQuarter and GetStartOfLastQuarter Problem Pin
edwardxu88819-Apr-09 11:29
memberedwardxu88819-Apr-09 11:29 
GeneralBugs Depending On Time Of Execution Pin
jason2653526-Dec-08 6:45
memberjason2653526-Dec-08 6:45 
GeneralDateTime with zero month and day Pin
ElGuroProgramador6-Aug-08 2:24
memberElGuroProgramador6-Aug-08 2:24 
GeneralRe: DateTime with zero month and day Pin
Michael Ceranski12-Aug-08 9:44
memberMichael Ceranski12-Aug-08 9:44 
GeneralThanks! Pin
JohnnyHax24-Apr-08 15:33
memberJohnnyHax24-Apr-08 15:33 
GeneralToad uses my library PinPopular
Michael Ceranski9-Apr-08 5:57
memberMichael Ceranski9-Apr-08 5:57 
GeneralDelphi x C# (Incompatibility) Pin
vinny_cwb14-Mar-08 2:27
membervinny_cwb14-Mar-08 2:27 
GeneralThank you. Pin
0112693-Oct-07 0:10
member0112693-Oct-07 0:10 
QuestionPlease help Pin
his_richness16-Nov-06 18:01
memberhis_richness16-Nov-06 18:01 
AnswerRe: Please help Pin
Michael Ceranski21-Nov-06 10:19
memberMichael Ceranski21-Nov-06 10:19 
GeneralSQL Server Trips Over This... Pin
MikeYeager12-Mar-06 10:50
memberMikeYeager12-Mar-06 10:50 
QuestionDate Format Problem Pin
Sivagnanam28-Feb-06 0:15
memberSivagnanam28-Feb-06 0:15 
GeneralMore suggestions Pin
Richard Cleaver28-Dec-05 3:42
memberRichard Cleaver28-Dec-05 3:42 
GeneralRe: More suggestions Pin
Omer van Kloeten28-Dec-05 10:06
memberOmer van Kloeten28-Dec-05 10:06 
QuestionWhat am I doing wrong? Pin
mdengler16-Dec-05 7:03
membermdengler16-Dec-05 7:03 
AnswerRe: What am I doing wrong? Pin
Michael Ceranski18-Dec-05 4:13
memberMichael Ceranski18-Dec-05 4:13 
GeneralRe: What am I doing wrong? Pin
mdengler18-Dec-05 4:50
membermdengler18-Dec-05 4:50 
GeneralRe: What am I doing wrong? Pin
KenGuru19-Dec-05 22:19
memberKenGuru19-Dec-05 22:19 
GeneralRe: What am I doing wrong? Pin
mdengler20-Dec-05 3:14
membermdengler20-Dec-05 3:14 
QuestionUnit Tests? Pin
Bil Simser11-May-05 23:31
memberBil Simser11-May-05 23:31 
GeneralSimplification Pin
Christian Falk12-Apr-05 20:50
memberChristian Falk12-Apr-05 20:50 
GeneralFiscal Year vs Calendar Year Pin
Darren Pruitt14-Mar-05 4:23
memberDarren Pruitt14-Mar-05 4:23 
GeneralRe: Fiscal Year vs Calendar Year Pin
Jani Giannoudis16-Mar-11 21:09
memberJani Giannoudis16-Mar-11 21:09 
GeneralC# standards Pin
Drew Noakes11-Mar-05 0:12
memberDrew Noakes11-Mar-05 0:12 
GeneralImprove readability... Pin
Alexander Turlov9-Mar-05 14:14
memberAlexander Turlov9-Mar-05 14:14 
GeneralRe: Improve readability... Pin
Drew Noakes11-Mar-05 0:01
memberDrew Noakes11-Mar-05 0:01 
GeneralSome useful methods else... Pin
Alexander Turlov9-Mar-05 14:02
memberAlexander Turlov9-Mar-05 14:02 
GeneralUNIX-like timestamp conversions Pin
Ionut FIlip7-Mar-05 19:45
sussIonut FIlip7-Mar-05 19:45 
GeneralRe: UNIX-like timestamp conversions Pin
Richard Deeming9-Mar-05 2:45
memberRichard Deeming9-Mar-05 2:45 
GeneralAnother simplification Pin
mav.northwind1-Mar-05 0:42
membermav.northwind1-Mar-05 0:42 
GeneralSimplification idea... Pin
Marc Brooks28-Feb-05 10:54
memberMarc Brooks28-Feb-05 10:54 
GeneralConfused Pin
Robert Rohde28-Feb-05 6:53
memberRobert Rohde28-Feb-05 6:53 
GeneralRe: Confused Pin
mceranski28-Feb-05 8:54
membermceranski28-Feb-05 8:54 
GeneralRe: Confused Pin
Drew Noakes11-Mar-05 0:16
memberDrew Noakes11-Mar-05 0:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160530.1 | Last Updated 19 Dec 2005
Article Copyright 2005 by Michael Ceranski
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid