CSharp examples for System:DateTime Day
Gets the number of days left in the time frame specified based on the date
/*//from w w w. ja v a 2 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> /// Gets the number of days left in the time frame specified based on the date /// </summary> /// <param name="Date">Date</param> /// <param name="TimeFrame">Time frame to calculate the number of days left</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 number of days left in the time frame</returns> public static int DaysLeftIn(this DateTime Date, TimeFrame TimeFrame, DateTime StartOfQuarter1, CultureInfo Culture = null) { if (TimeFrame != TimeFrame.Quarter) return Date.DaysLeftIn(TimeFrame, Culture); Culture = Culture.Check(CultureInfo.CurrentCulture); return Date.DaysIn(TimeFrame.Quarter, StartOfQuarter1, Culture) - (Date.DayOfYear - StartOfQuarter1.DayOfYear); } #endregion #region DaysLeftIn /// <summary> /// Gets the number of days left in the time frame specified based on the date /// </summary> /// <param name="Date">Date</param> /// <param name="TimeFrame">Time frame to calculate the number of days left</param> /// <param name="Culture">Culture to use for calculating (defaults to the current culture)</param> /// <returns>The number of days left in the time frame</returns> public static int DaysLeftIn(this DateTime Date, TimeFrame TimeFrame, CultureInfo Culture = null) { Culture = Culture.Check(CultureInfo.CurrentCulture); if (TimeFrame == TimeFrame.Day) return 1; if (TimeFrame == TimeFrame.Week) return 7 - ((int)Date.DayOfWeek + 1); if (TimeFrame == TimeFrame.Month) return Date.DaysIn(TimeFrame.Month, Culture) - Date.Day; if (TimeFrame == TimeFrame.Quarter) return Date.DaysIn(TimeFrame.Quarter, Culture) - (Date.DayOfYear - Date.BeginningOf(TimeFrame.Quarter, Culture).DayOfYear); return Date.DaysIn(TimeFrame.Year, Culture) - Date.DayOfYear; } /// <summary> /// Gets the number of days in the time frame specified based on the date /// </summary> /// <param name="Date">Date</param> /// <param name="TimeFrame">Time frame to calculate the number of days from</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 number of days in the time frame</returns> public static int DaysIn(this DateTime Date, TimeFrame TimeFrame, DateTime StartOfQuarter1, CultureInfo Culture = null) { if (TimeFrame != TimeFrame.Quarter) Date.DaysIn(TimeFrame, Culture); Culture = Culture.Check(CultureInfo.CurrentCulture); return Date.EndOf(TimeFrame.Quarter, Culture).DayOfYear - StartOfQuarter1.DayOfYear; } #endregion #region DaysIn /// <summary> /// Gets the number of days in the time frame specified based on the date /// </summary> /// <param name="Date">Date</param> /// <param name="TimeFrame">Time frame to calculate the number of days from</param> /// <param name="Culture">Culture to use for calculating (defaults to the current culture)</param> /// <returns>The number of days in the time frame</returns> public static int DaysIn(this DateTime Date, TimeFrame TimeFrame, CultureInfo Culture = null) { Culture = Culture.Check(CultureInfo.CurrentCulture); if (TimeFrame == TimeFrame.Day) return 1; if (TimeFrame == TimeFrame.Week) return 7; if (TimeFrame == TimeFrame.Month) return Culture.Calendar.GetDaysInMonth(Date.Year, Date.Month); if (TimeFrame == TimeFrame.Quarter) return Date.EndOf(TimeFrame.Quarter, Culture).DayOfYear - Date.BeginningOf(TimeFrame.Quarter, Culture).DayOfYear; return Culture.Calendar.GetDaysInYear(Date.Year); } /// <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); } }