Beginning of a specific time frame - CSharp System

CSharp examples for System:DateTime Time

Description

Beginning of a specific time frame

Demo Code

/*/*from   ww  w. j a  va2 s.c om*/
Copyright (c) 2012 <a href="http://www.gutgames.com">James Craig</a>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/
using System.Globalization;
using System.Diagnostics.Contracts;
using System;

public class Main{
        /// <summary>
        /// Beginning of a specific time frame
        /// </summary>
        /// <param name="Date">Date to base off of</param>
        /// <param name="TimeFrame">Time frame to use</param>
        /// <param name="Culture">Culture to use for calculating (defaults to the current culture)</param>
        /// <param name="StartOfQuarter1">Start of the first quarter</param>
        /// <returns>The beginning of a specific time frame</returns>
        public static DateTime BeginningOf(this DateTime Date, TimeFrame TimeFrame, DateTime StartOfQuarter1, CultureInfo Culture = null)
        {
            if (TimeFrame != TimeFrame.Quarter)
                return Date.BeginningOf(TimeFrame, Culture);
            Culture = Culture.Check(CultureInfo.CurrentCulture);
            if (Date.Between(StartOfQuarter1, StartOfQuarter1.AddMonths(3).AddDays(-1).EndOf(TimeFrame.Day, CultureInfo.CurrentCulture)))
                return StartOfQuarter1.Date;
            else if (Date.Between(StartOfQuarter1.AddMonths(3), StartOfQuarter1.AddMonths(6).AddDays(-1).EndOf(TimeFrame.Day, CultureInfo.CurrentCulture)))
                return StartOfQuarter1.AddMonths(3).Date;
            else if (Date.Between(StartOfQuarter1.AddMonths(6), StartOfQuarter1.AddMonths(9).AddDays(-1).EndOf(TimeFrame.Day, CultureInfo.CurrentCulture)))
                return StartOfQuarter1.AddMonths(6).Date;
            return StartOfQuarter1.AddMonths(9).Date;
        }
        #endregion

        #region BeginningOf

        /// <summary>
        /// Beginning of a specific time frame
        /// </summary>
        /// <param name="Date">Date to base off of</param>
        /// <param name="TimeFrame">Time frame to use</param>
        /// <param name="Culture">Culture to use for calculating (defaults to the current culture)</param>
        /// <returns>The beginning of a specific time frame</returns>
        public static DateTime BeginningOf(this DateTime Date, TimeFrame TimeFrame, CultureInfo Culture = null)
        {
            Culture = Culture.Check(CultureInfo.CurrentCulture);
            if (TimeFrame == TimeFrame.Day)
                return Date.Date;
            if (TimeFrame == TimeFrame.Week)
                return Date.AddDays(Culture.DateTimeFormat.FirstDayOfWeek - Date.DayOfWeek).Date;
            if (TimeFrame == TimeFrame.Month)
                return new DateTime(Date.Year, Date.Month, 1);
            if (TimeFrame == TimeFrame.Quarter)
                return Date.BeginningOf(TimeFrame.Quarter, Date.BeginningOf(TimeFrame.Year, Culture), Culture);
            return new DateTime(Date.Year, 1, 1);
        }
        /// <summary>
        /// End of a specific time frame
        /// </summary>
        /// <param name="Date">Date to base off of</param>
        /// <param name="TimeFrame">Time frame to use</param>
        /// <param name="Culture">Culture to use for calculating (defaults to the current culture)</param>
        /// <param name="StartOfQuarter1">Start of the first quarter</param>
        /// <returns>The end of a specific time frame (TimeFrame.Day is the only one that sets the time to 12:59:59 PM, all else are the beginning of the day)</returns>
        public static DateTime EndOf(this DateTime Date, TimeFrame TimeFrame, DateTime StartOfQuarter1, CultureInfo Culture = null)
        {
            if (TimeFrame != TimeFrame.Quarter)
                return Date.EndOf(TimeFrame, Culture);
            Culture = Culture.Check(CultureInfo.CurrentCulture);
            if (Date.Between(StartOfQuarter1, StartOfQuarter1.AddMonths(3).AddDays(-1).EndOf(TimeFrame.Day, Culture)))
                return StartOfQuarter1.AddMonths(3).AddDays(-1).Date;
            else if (Date.Between(StartOfQuarter1.AddMonths(3), StartOfQuarter1.AddMonths(6).AddDays(-1).EndOf(TimeFrame.Day, Culture)))
                return StartOfQuarter1.AddMonths(6).AddDays(-1).Date;
            else if (Date.Between(StartOfQuarter1.AddMonths(6), StartOfQuarter1.AddMonths(9).AddDays(-1).EndOf(TimeFrame.Day, Culture)))
                return StartOfQuarter1.AddMonths(9).AddDays(-1).Date;
            return StartOfQuarter1.AddYears(1).AddDays(-1).Date;
        }
        #endregion

        #region EndOf

        /// <summary>
        /// End of a specific time frame
        /// </summary>
        /// <param name="Date">Date to base off of</param>
        /// <param name="TimeFrame">Time frame to use</param>
        /// <param name="Culture">Culture to use for calculating (defaults to the current culture)</param>
        /// <returns>The end of a specific time frame (TimeFrame.Day is the only one that sets the time to 12:59:59 PM, all else are the beginning of the day)</returns>
        public static DateTime EndOf(this DateTime Date, TimeFrame TimeFrame, CultureInfo Culture = null)
        {
            Culture = Culture.Check(CultureInfo.CurrentCulture);
            if (TimeFrame == TimeFrame.Day)
                return new DateTime(Date.Year, Date.Month, Date.Day, 23, 59, 59);
            if (TimeFrame == TimeFrame.Week)
                return Date.BeginningOf(TimeFrame.Week, Culture).AddDays(6);
            if (TimeFrame == TimeFrame.Month)
                return Date.AddMonths(1).BeginningOf(TimeFrame.Month, Culture).AddDays(-1).Date;
            if (TimeFrame == TimeFrame.Quarter)
                return Date.EndOf(TimeFrame.Quarter, Date.BeginningOf(TimeFrame.Year, Culture), Culture);
            return new DateTime(Date.Year, 12, 31);
        }
}

Related Tutorials