Skip to content

Instantly share code, notes, and snippets.

@xrisdoc
Last active June 20, 2017 09:06
Show Gist options
  • Save xrisdoc/cdbb715f71e613243cc5107e59826d2f to your computer and use it in GitHub Desktop.
Save xrisdoc/cdbb715f71e613243cc5107e59826d2f to your computer and use it in GitHub Desktop.
DateTimeExtensions
using System;
namespace Xrisdoc.Extensions
{
public static class DateTimeExtensions
{
/// <summary>
/// Identifies whether or not the specified period is a monthly period
/// </summary>
/// <param name="startDate">The start date of the period</param>
/// <param name="endDate">The end date of the period</param>
/// <returns>A boolean value indicating if the specified period is a monthly period or not</returns>
public static bool IsMonthPeriod(DateTime startDate, DateTime endDate)
{
if (startDate.IsSameMonthAs(endDate) && startDate.IsFirstDayOfMonth() && endDate.IsLastDayOfMonth())
{
return true;
}
return false;
}
/// <summary>
/// Gets the age in relation to the current date
/// </summary>
/// <param name="date"></param>
/// <returns>An integer representing the relevant age</returns>
public static int GetCurrentAge(this DateTime date)
{
DateTime today = DateTime.Today;
int age = today.Year - date.Year;
if (date > today.AddYears(-age)) age--;
return age;
}
/// <summary>
/// Gets the date for the first day of the current month
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public static DateTime GetFirstDayOfMonth(this DateTime date)
{
return new DateTime(date.Year, date.Month, 1);
}
/// <summary>
/// Gets the date for the first day of the specified month
/// </summary>
/// <param name="month">The month that the first day of the month is to be obtained for</param>
/// <param name="year">The year that the first day of the month is to be obtained for</param>
/// <returns></returns>
public static DateTime GetFirstDayOfMonth(int month, int year)
{
return new DateTime(year, month, 1);
}
/// <summary>
/// Gets the date for the last day of the current month
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public static DateTime GetLastDayOfMonth(this DateTime date)
{
return new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month));
}
/// <summary>
/// Gets the date for the last day of the specified month
/// </summary>
/// <param name="month">The month that the last day of the month is to be obtained for</param>
/// <param name="year">The year that the last day of the month is to be obtained for</param>
/// <returns></returns>
public static DateTime GetLastDayOfMonth(int month, int year)
{
return new DateTime(year, month, DateTime.DaysInMonth(month, year));
}
/// <summary>
/// Checks to see if the current date is the first day of the month
/// </summary>
/// <param name="date"></param>
/// <returns>True if it is the firts day of the month. Otherwise return false</returns>
public static bool IsFirstDayOfMonth(this DateTime date)
{
return date.Day == 1;
}
/// <summary>
/// Checks to see if the current date is the last day of the month
/// </summary>
/// <param name="date"></param>
/// <returns>True if it is the last day of the month. Otherwise return false</returns>
public static bool IsLastDayOfMonth(this DateTime date)
{
return date.CompareTo(GetLastDayOfMonth(date)) == 0;
}
/// <summary>
///
/// </summary>
/// <param name="date"></param>
/// <param name="dateToComapreTo"></param>
/// <returns></returns>
public static bool IsSameMonthAs(this DateTime date, DateTime dateToComapreTo)
{
return date.Month == dateToComapreTo.Month && date.Year == dateToComapreTo.Year;
}
/// <summary>
///
/// </summary>
/// <param name="date"></param>
/// <param name="month"></param>
/// <param name="year"></param>
/// <returns></returns>
public static bool IsSameMonthAs(this DateTime date, int month, int year)
{
return date.Month == month && date.Year == year;
}
/// <summary>
///
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public static bool IsCurrentMonth(this DateTime date)
{
return date.Month == DateTime.Today.Month && date.Year == DateTime.Today.Year;
}
/// <summary>
///
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public static bool IsCurrentMonth(int month, int year)
{
return month == DateTime.Today.Month && year == DateTime.Today.Year;
}
/// <summary>
///
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public static bool IsCurrentYear(this DateTime date)
{
return date.Year == DateTime.Today.Year;
}
/// <summary>
///
/// </summary>
/// <param name="year"></param>
/// <returns></returns>
public static bool IsCurrentYear(int year)
{
return year == DateTime.Today.Year;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment