[ExtensionMethods] System.DateTime

Posted by Ahmed Tarek Hasan on 11/04/2012 08:06:00 PM with 2 comments
These are extension methods for "System.DateTime" class. Some are written by me and the rest are collected from other sources. Hope you find them useful :)


using System;
 
namespace DevelopmentSimplyPut.ExtensionMethods.DateTimeEM
{
 /// <summary>
 /// DateTime Extensions
 /// </summary>
 public static class DateTimeExtensionMethods
 {
  /// <summary>
  /// Elapseds the time.
  /// </summary>
  /// <param name="datetime">The datetime.</param>
  /// <returns>TimeSpan</returns>
  public static TimeSpan ext_Elapsed(this DateTime datetime)
  {
   return DateTime.Now - datetime;
  }
  /// <summary>
  /// Weeks the of year.
  /// </summary>
  /// <param name="datetime">The datetime.</param>
  /// <param name="weekrule">The weekrule.</param>
  /// <param name="firstDayOfWeek">The first day of week.</param>
  /// <returns></returns>
  public static int ext_WeekOfYear(this DateTime datetime, System.Globalization.CalendarWeekRule weekrule, DayOfWeek firstDayOfWeek)
  {
   System.Globalization.CultureInfo ciCurr = System.Globalization.CultureInfo.CurrentCulture;
   return ciCurr.Calendar.GetWeekOfYear(datetime, weekrule, firstDayOfWeek);
  }
  /// <summary>
  /// Weeks the of year.
  /// </summary>
  /// <param name="datetime">The datetime.</param>
  /// <param name="firstDayOfWeek">The first day of week.</param>
  /// <returns></returns>
  public static int ext_WeekOfYear(this DateTime datetime, DayOfWeek firstDayOfWeek)
  {
   System.Globalization.DateTimeFormatInfo dateinf = new System.Globalization.DateTimeFormatInfo();
   System.Globalization.CalendarWeekRule weekrule = dateinf.CalendarWeekRule;
   return ext_WeekOfYear(datetime, weekrule, firstDayOfWeek);
  }
  /// <summary>
  /// Weeks the of year.
  /// </summary>
  /// <param name="datetime">The datetime.</param>
  /// <param name="weekrule">The weekrule.</param>
  /// <returns></returns>
  public static int ext_WeekOfYear(this DateTime datetime, System.Globalization.CalendarWeekRule weekrule)
  {
   System.Globalization.DateTimeFormatInfo dateinf = new System.Globalization.DateTimeFormatInfo();
   DayOfWeek firstDayOfWeek = dateinf.FirstDayOfWeek;
   return ext_WeekOfYear(datetime, weekrule, firstDayOfWeek);
  }
  /// <summary>
  /// Weeks the of year.
  /// </summary>
  /// <param name="datetime">The datetime.</param>
  /// <param name="weekrule">The weekrule.</param>
  /// <returns></returns>
  public static int ext_WeekOfYear(this DateTime datetime)
  {
   System.Globalization.DateTimeFormatInfo dateinf = new System.Globalization.DateTimeFormatInfo();
   System.Globalization.CalendarWeekRule weekrule = dateinf.CalendarWeekRule;
   DayOfWeek firstDayOfWeek = dateinf.FirstDayOfWeek;
   return ext_WeekOfYear(datetime, weekrule, firstDayOfWeek);
  }
  /// <summary>
  /// Gets the date time for day of week.
  /// </summary>
  /// <param name="datetime">The datetime.</param>
  /// <param name="day">The day.</param>
  /// <param name="firstDayOfWeek">The first day of week.</param>
  /// <returns></returns>
  public static DateTime ext_GetDateTimeForDayOfWeek(this DateTime datetime, DayOfWeek day, DayOfWeek firstDayOfWeek)
  {
   int current = DaysFromFirstDayOfWeek(datetime.DayOfWeek, firstDayOfWeek);
   int resultday = DaysFromFirstDayOfWeek(day, firstDayOfWeek);
   return datetime.AddDays(resultday - current);
  }
  /// <summary>
  /// Gets the date time for day of week.
  /// </summary>
  /// <param name="datetime">The datetime.</param>
  /// <param name="day">The day</param>
  /// <returns></returns>
  public static DateTime ext_GetDateTimeForDayOfWeek(this DateTime datetime, DayOfWeek day)
  {
   System.Globalization.DateTimeFormatInfo dateinf = new System.Globalization.DateTimeFormatInfo();
   DayOfWeek firstDayOfWeek = dateinf.FirstDayOfWeek;
   return ext_GetDateTimeForDayOfWeek(datetime, day, firstDayOfWeek);
  }
  /// <summary>
  /// Firsts the date time of week.
  /// </summary>
  /// <param name="datetime">The datetime.</param>
  /// <returns></returns>
  public static DateTime ext_FirstDateTimeOfWeek(this DateTime datetime)
  {
   System.Globalization.DateTimeFormatInfo dateinf = new System.Globalization.DateTimeFormatInfo();
   DayOfWeek firstDayOfWeek = dateinf.FirstDayOfWeek;
   return ext_FirstDateTimeOfWeek(datetime, firstDayOfWeek);
  }
  /// <summary>
  /// Firsts the date time of week.
  /// </summary>
  /// <param name="datetime">The datetime.</param>
  /// <param name="firstDayOfWeek">The first day of week.</param>
  /// <returns></returns>
  public static DateTime ext_FirstDateTimeOfWeek(this DateTime datetime, DayOfWeek firstDayOfWeek)
  {
   return datetime.AddDays(-DaysFromFirstDayOfWeek(datetime.DayOfWeek, firstDayOfWeek));
  }
  /// <summary>
  /// Days from first day of week.
  /// </summary>
  /// <param name="current">The current.</param>
  /// <param name="firstDayOfWeek">The first day of week.</param>
  /// <returns></returns>
  private static int DaysFromFirstDayOfWeek(DayOfWeek current, DayOfWeek firstDayOfWeek)
  {
   //Sunday = 0,Monday = 1,...,Saturday = 6
   int daysbetween = current - firstDayOfWeek;
   if (daysbetween < 0) daysbetween = 7 + daysbetween;
   return daysbetween;
  }
  /// <summary>
  /// Gets the string representation of a date if available
  /// </summary>
  /// <param name="datetime">The date</param>
  /// <param name="defaultvalue">The default value</param>
  /// <returns></returns>
  public static string ext_GetValueOrDefaultToString(this DateTime? datetime, string defaultvalue)
  {
   if (datetime == null) return defaultvalue;
   return datetime.Value.ToString();
  }
  /// <summary>
  /// Gets the string representation of a date if available
  /// </summary>
  /// <param name="datetime">The date</param>
  /// <param name="format">The format of the string representation of the date</param>
  /// <param name="defaultvalue">The default value</param>
  /// <returns></returns>
  public static string ext_GetValueOrDefaultToString(this DateTime? datetime, string format, string defaultvalue)
  {
   if (datetime == null) return defaultvalue;
   return datetime.Value.ToString(format);
  }
 }
}