public static class DateExtensions { ////// DateDiff in SQL style. /// Datepart implemented: /// "year" (abbr. "yy", "yyyy"), /// "quarter" (abbr. "qq", "q"), /// "month" (abbr. "mm", "m"), /// "day" (abbr. "dd", "d"), /// "week" (abbr. "wk", "ww"), /// "hour" (abbr. "hh"), /// "minute" (abbr. "mi", "n"), /// "second" (abbr. "ss", "s"), /// "millisecond" (abbr. "ms"). /// /// /// ///public static Int64 DateDiff(this DateTime StartDate, String DatePart, DateTime EndDate) { Int64 DateDiffVal = 0; System.Globalization.Calendar cal = System.Threading.Thread.CurrentThread.CurrentCulture.Calendar; TimeSpan ts = new TimeSpan(EndDate.Ticks - StartDate.Ticks); switch (DatePart.ToLower().Trim()) { #region year case "year": case "yy": case "yyyy": DateDiffVal = (Int64)(cal.GetYear(EndDate) - cal.GetYear(StartDate)); break; #endregion #region quarter case "quarter": case "qq": case "q": DateDiffVal = (Int64)((((cal.GetYear(EndDate) - cal.GetYear(StartDate)) * 4) + ((cal.GetMonth(EndDate) - 1) / 3)) - ((cal.GetMonth(StartDate) - 1) / 3)); break; #endregion #region month case "month": case "mm": case "m": DateDiffVal = (Int64)(((cal.GetYear(EndDate) - cal.GetYear(StartDate)) * 12 + cal.GetMonth(EndDate)) - cal.GetMonth(StartDate)); break; #endregion #region day case "day": case "d": case "dd": DateDiffVal = (Int64)ts.TotalDays; break; #endregion #region week case "week": case "wk": case "ww": DateDiffVal = (Int64)(ts.TotalDays / 7); break; #endregion #region hour case "hour": case "hh": DateDiffVal = (Int64)ts.TotalHours; break; #endregion #region minute case "minute": case "mi": case "n": DateDiffVal = (Int64)ts.TotalMinutes; break; #endregion #region second case "second": case "ss": case "s": DateDiffVal = (Int64)ts.TotalSeconds; break; #endregion #region millisecond case "millisecond": case "ms": DateDiffVal = (Int64)ts.TotalMilliseconds; break; #endregion default: throw new Exception(String.Format("DatePart \"{0}\" is unknown", DatePart)); } return DateDiffVal; } public static string ToStringDDMMYYYYFromDate(this DateTime date) { if (date == null) { return ""; } else { return date.ToString("dd/MM/yyyy"); } } public static string ToStringDDMMYYYYFromDate(this DateTime? date) { if (date == null) { return ""; } else { return ((DateTime)date).ToString("dd-MM-yyyy"); } } static public int Age(this DateTime dateOfBirth) { if (DateTime.Today.Month < dateOfBirth.Month || DateTime.Today.Month == dateOfBirth.Month && DateTime.Today.Day < dateOfBirth.Day) { return DateTime.Today.Year - dateOfBirth.Year - 1; } else return DateTime.Today.Year - dateOfBirth.Year; } public static string ToFriendlyDateString(this DateTime Date) { string FormattedDate = ""; if (Date.Date == DateTime.Today) { FormattedDate = "Today"; } else if (Date.Date == DateTime.Today.AddDays(-1)) { FormattedDate = "Yesterday"; } else if (Date.Date > DateTime.Today.AddDays(-6)) { // *** Show the Day of the week FormattedDate = Date.ToString("dddd").ToString(); } else { FormattedDate = Date.ToString("MMMM dd, yyyy"); } //append the time portion to the output FormattedDate += " @ " + Date.ToString("t").ToLower(); return FormattedDate; } public static bool IsWeekend(this DayOfWeek d) { return !d.IsWeekday(); } public static bool IsWeekday(this DayOfWeek d) { switch (d) { case DayOfWeek.Sunday: case DayOfWeek.Saturday: return false; default: return true; } } public static DateTime AddWorkdays(this DateTime d, int days) { // start from a weekday while (d.DayOfWeek.IsWeekday()) d = d.AddDays(1.0); for (int i = 0; i < days; ++i) { d = d.AddDays(1.0); while (d.DayOfWeek.IsWeekday()) d = d.AddDays(1.0); } return d; } public static string FirstDayOfMonth(this DateTime date) { return new DateTime(date.Year, date.Month, 1).ToString("MM/dd/yyyy"); } public static string LastDayOfMonth(this DateTime date) { return new DateTime(DateTime.Now.Year, DateTime.Now.Month + 1, 1).AddDays(-1).ToString("MM/dd/yyyy"); } /// /// Converts a regular DateTime to a RFC822 date string. /// ///The specified date formatted as a RFC822 date string. public static string ToRFC822DateString(this DateTime date) { int offset = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now).Hours; string timeZone = "+" + offset.ToString().PadLeft(2, '0'); if (offset < 0) { int i = offset * -1; timeZone = "-" + i.ToString().PadLeft(2, '0'); } return date.ToString("ddd, dd MMM yyyy HH:mm:ss " + timeZone.PadRight(5, '0'), System.Globalization.CultureInfo.GetCultureInfo("en-US")); } }
Monday, January 30, 2012
Best Extension Methods: Date and Time
Best Extension Methods for Date and time
Share This!
Labels:
C#.NET,
Extension Methods
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment