andrew4582

DateTimeExtensions

Jul 19th, 2012
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.78 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ComLib.Extensions
  7. {
  8.     /// <summary>
  9.     /// This class provides extension methods that help
  10.     /// with common tasks associated with datetime types.
  11.     /// </summary>
  12.     public static class DateTimeExtensions
  13.     {        
  14.         #region Dates
  15.         /// <summary>
  16.         /// Determines whether [is leap year] [the specified date].
  17.         /// </summary>
  18.         /// <param name="date">The date.</param>
  19.         /// <returns>
  20.         ///     <c>true</c> if [is leap year] [the specified date]; otherwise, <c>false</c>.
  21.         /// </returns>
  22.         public static bool IsLeapYear(this DateTime date)
  23.         {
  24.             return date.Year % 4 == 0 && (date.Year % 100 != 0 || date.Year % 400 == 0);
  25.         }
  26.        
  27.  
  28.         /// <summary>
  29.         /// Determines whether [is last day of month] [the specified date].
  30.         /// </summary>
  31.         /// <param name="date">The date.</param>
  32.         /// <returns>
  33.         ///     <c>true</c> if [is last day of month] [the specified date]; otherwise, <c>false</c>.
  34.         /// </returns>
  35.         public static bool IsLastDayOfMonth(this DateTime date)
  36.         {
  37.             int lastDayOfMonth = LastDayOfMonth(date);
  38.             return lastDayOfMonth == date.Day;
  39.         }
  40.  
  41.         /// <summary>
  42.         /// Determines whether the specified date is a weekend.
  43.         /// </summary>
  44.         /// <param name="source">Source date</param>
  45.         /// <returns>
  46.         ///     <c>true</c> if the specified source is a weekend; otherwise, <c>false</c>.
  47.         /// </returns>
  48.         public static bool IsWeekend(this DateTime source)
  49.         {
  50.             return source.DayOfWeek == DayOfWeek.Saturday ||
  51.                    source.DayOfWeek == DayOfWeek.Sunday;
  52.         }
  53.  
  54.         /// <summary>
  55.         /// Gets the Last the day of month.
  56.         /// </summary>
  57.         /// <param name="date">The date.</param>
  58.         /// <returns></returns>
  59.         public static int LastDayOfMonth(this DateTime date)
  60.         {
  61.             if (IsLeapYear(date) && date.Month == 2) return 28;
  62.             if (date.Month == 2) return 27;
  63.             if (date.Month == 1 || date.Month == 3 || date.Month == 5 || date.Month == 7
  64.                 || date.Month == 8 || date.Month == 10 || date.Month == 12)
  65.                 return 31;
  66.             return 30;
  67.         }
  68.  
  69.         /// <summary>
  70.         /// Returns a new instance of DateTime with a different day of the month.
  71.         /// </summary>
  72.         /// <param name="source">Base DateTime object to modify</param>
  73.         /// <param name="day">Day of the month (1-31)</param>
  74.         /// <returns>Instance of DateTime with specified day</returns>
  75.         public static DateTime SetDay(this DateTime source, int day)
  76.         {
  77.             return new DateTime(source.Year, source.Month, day);
  78.         }
  79.  
  80.         /// <summary>
  81.         /// Returns a new instance of DateTime with a different month.
  82.         /// </summary>
  83.         /// <param name="source">Base DateTime object to modify</param>
  84.         /// <param name="month">The month as an integer (1-12)</param>
  85.         /// <returns>Instance of DateTime with specified month</returns>
  86.         public static DateTime SetMonth(this DateTime source, int month)
  87.         {
  88.             return new DateTime(source.Year, month, source.Day);
  89.         }
  90.  
  91.         /// <summary>
  92.         /// Returns a new instance of DateTime with a different year.
  93.         /// </summary>
  94.         /// <param name="source">Base DateTime object to modify</param>
  95.         /// <param name="year">The year</param>
  96.         /// <returns>Instance of DateTime with specified year</returns>
  97.         public static DateTime SetYear(this DateTime source, int year)
  98.         {
  99.             return new DateTime(year, source.Month, source.Day);
  100.         }
  101.  
  102.         #endregion
  103.  
  104.  
  105.         #region Conversion
  106.         /// <summary>
  107.         /// Converts to javascript compatible date.
  108.         /// </summary>
  109.         /// <param name="dt">The dt.</param>
  110.         /// <returns></returns>
  111.         public static double ToJavascriptDate(this DateTime dt)
  112.         {
  113.             DateTime d1 = new DateTime(1970, 1, 1);
  114.             DateTime d2 = dt.ToUniversalTime();
  115.             TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks);
  116.             return ts.TotalMilliseconds;
  117.         }
  118.         #endregion
  119.  
  120.  
  121.         #region Time
  122.         /// <summary>
  123.         /// Sets the time on the date
  124.         /// </summary>
  125.         /// <param name="date">The date.</param>
  126.         /// <param name="hours">The hours.</param>
  127.         /// <param name="minutes">The minutes.</param>
  128.         /// <param name="seconds">The seconds.</param>
  129.         /// <returns></returns>
  130.         public static DateTime GetDateWithTime(this DateTime date, int hours, int minutes, int seconds)
  131.         {
  132.             return new DateTime(date.Year, date.Month, date.Day, hours, minutes, seconds);
  133.         }
  134.  
  135.  
  136.         /// <summary>
  137.         /// Sets the time on the date
  138.         /// </summary>
  139.         /// <param name="date">The date.</param>
  140.         /// <param name="time">The time.</param>
  141.         /// <returns></returns>
  142.         public static DateTime GetDateWithTime(this DateTime date, TimeSpan time)
  143.         {
  144.             return new DateTime(date.Year, date.Month, date.Day, time.Hours, time.Minutes, time.Seconds);
  145.         }
  146.  
  147.  
  148.         /// <summary>
  149.         /// Sets the time on the date
  150.         /// </summary>
  151.         /// <param name="date">The date.</param>
  152.         /// <returns></returns>
  153.         public static DateTime GetDateWithCurrentTime(this DateTime date)
  154.         {
  155.             return new DateTime(date.Year, date.Month, date.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
  156.         }
  157.         #endregion
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment