You inspired me to contribute the
DateTime
extensions methods that I use in my programs. (But use at your own risk!). Thank you.
public static class DateMan
{
public static int Age(this DateTime dob)
{
return (int)((DateTime.Now - dob).Days / 365.25);
}
public static bool IsMorning(this DateTime time)
{
return time.TimeOfDay < new DateTime(2000, 1, 1, 12, 0, 0).TimeOfDay;
}
public static bool IsAfternoon(this DateTime time)
{
return time.TimeOfDay >= new DateTime(2000, 1, 1, 12, 0, 0).TimeOfDay;
}
public static DateTime StartOfNextHour(this DateTime time)
{
DateTime result = time.AddHours(1).AddMinutes(-time.Minute).AddSeconds(-time.Second);
return result;
}
public static DateTime StartOfNextDay(this DateTime time)
{
DateTime result = time.AddDays(1);
return result.Date;
}
public static DateTime StartOfThisWeek(this DateTime time)
{
DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
int Days = time.DayOfWeek - dfi.FirstDayOfWeek;
Days = (7 + Days) % 7;
return time.AddDays(-Days);
}
public static DateTime StartOfNextWeek(this DateTime time)
{
return time.StartOfThisWeek().AddDays(7);
}
public static DateTime StartOfNextMonth(this DateTime time)
{
DateTime result = time.AddMonths(1);
return new DateTime(result.Year, result.Month, 1);
}
public static DateTime StartOfThisMonth(this DateTime time)
{
return new DateTime(time.Year, time.Month, 1);
}
public static DateTime EndOfThisMonth(this DateTime time)
{
DateTime result = time.AddMonths(1);
return new DateTime(result.Year, result.Month, 1).AddDays(-1);
}
public static DateTime StartOfNextYear(this DateTime time)
{
DateTime result = time.AddYears(1);
return new DateTime(result.Year, 1, 1);
}
public static int GetMonthByDescription(this string month)
{
if (month.IsInteger())
{
return int.Parse(month);
}
if (month.Trim().Length <= 2)
{
throw new Exception("Input not a valid month representation.");
}
int result = 0;
switch (month.Left(3).ToLower())
{
case "jan":
result = 1;
break;
case "feb":
result = 2;
break;
case "mar":
result = 3;
break;
case "apr":
result = 4;
break;
case "may":
result = 5;
break;
case "jun":
result = 6;
break;
case "jul":
result = 7;
break;
case "aug":
result = 8;
break;
case "sep":
result = 9;
break;
case "oct":
result = 10;
break;
case "nov":
result = 11;
break;
case "dec":
result = 12;
break;
}
return result;
}
public static void MiliSecParse(int inputMiliSecs, out int days, out int hours, out int minutes, out int seconds, out int miliSeconds)
{
TimeSpan timeSpan = new TimeSpan(0, 0, 0, 0, inputMiliSecs);
MiliSecParse(timeSpan, out days, out hours, out minutes, out seconds, out miliSeconds);
}
public static void MiliSecParse(double inputMiliSecs, out int days, out int hours, out int minutes, out int seconds, out int miliSeconds)
{
TimeSpan timeSpan = new TimeSpan(0, 0, 0, 0, (int)inputMiliSecs);
MiliSecParse(timeSpan, out days, out hours, out minutes, out seconds, out miliSeconds);
}
public static void MiliSecParse(TimeSpan timeSpan, out int days, out int hours, out int minutes, out int seconds, out int miliSeconds)
{
days = timeSpan.Days;
hours = timeSpan.Hours;
minutes = timeSpan.Minutes;
seconds = timeSpan.Seconds;
miliSeconds = timeSpan.Milliseconds;
}
public static int DaysInMonth(this DateTime date)
{
int result = 31;
switch (date.Month)
{
case 2:
result = date.IsLeapYear() ? 29 : 28;
break;
case 4:
result = 30;
break;
case 6:
result = 30;
break;
case 9:
result = 30;
break;
case 11:
result = 30;
break;
}
return result;
}
public static bool IsLeapYear(this DateTime date)
{
int year = date.Year;
return ((year.IsMultipleOf(4) && !year.IsMultipleOf(100)) || year.IsMultipleOf(400));
}
public static DateTime GetPCTimeNoDaylightSaving()
{
DateTime result = DateTime.Now;
TimeZone timeZone = TimeZone.CurrentTimeZone;
if (timeZone.IsDaylightSavingTime(result))
{
DaylightTime daylightTime = timeZone.GetDaylightChanges(result.Year);
result -= daylightTime.Delta;
}
return result;
}
public static bool IsDaylightSavingTime()
{
return TimeZone.CurrentTimeZone.IsDaylightSavingTime(DateTime.Now);
}
public static bool IsDaylightSavingTime(DateTime date)
{
return TimeZone.CurrentTimeZone.IsDaylightSavingTime(date);
}
public static int Quarter(this DateTime date)
{
return (date.Month + 2) / 3;
}
}