The documentation comes from the Markdown files in the source code, so is always up-to-date but available only in English. Enjoy!
Returns whether a date is in the interval [MinDate, MaxDate[ (max date not included). As usual it follows a C-interval convention.
public static bool IsInInterval(this DateTime date, DateTime minDate, DateTime maxDate)
{
return minDate <= date && date < maxDate;
}
public static bool IsInInterval(this DateTime date, DateTime minDate, DateTime? maxDate)
{
return minDate <= date && (maxDate == null || date < maxDate);
}
public static bool IsInInterval(this DateTime date, DateTime? minDate, DateTime? maxDate)
{
return (minDate == null || minDate <= date) &&
(maxDate == null || date < maxDate);
}
Similar but also asserts that all the three arguments have no time part (hours minutes and seconds).
public static bool IsInDateInterval(this DateTime date, DateTime minDate, DateTime maxDate)
public static bool IsInDateInterval(this DateTime date, DateTime minDate, DateTime? maxDate)
public static bool IsInDateInterval(this DateTime date, DateTime? minDate, DateTime? maxDate)
Returns how many whole years there are between dates (Not year transitions):
public static int YearsTo(this DateTime min, DateTime max)
It does not takes hours, minutes ... into account.
Example:
new DateTime(1999, 12, 31).YearsTo(new DateTime(2000, 1, 1)); //Returns 0, no whole year
new DateTime(1999, 12, 31).YearsTo(new DateTime(2001, 1, 1)); //Returns 1
Returns how many whole months there are between Dates (Calendar months, not Days divided by something between 28 and 31, or month transitions).
public static int MonthsTo(this DateTime min, DateTime max)
It does not takes hours, minutes ... into account.
Example:
new DateTime(2009, 2, 4).MonthsTo(new DateTime(2009, 3, 3)); //Returns 0
new DateTime(2009, 2, 4).MonthsTo(new DateTime(2009, 3, 4)); //Returns 1
DateSpan is a calendar-relative version of TimeSpan for big time spans (Years, Months, and Days). That means that it's not possible to be transformed to TimeSpan without a calendar context.
public struct DateSpan
{
public readonly int Years;
public readonly int Months;
public readonly int Days;
public DateSpan(int years, int months, int days){...}
public static DateSpan FromToDates(DateTime min, DateTime max){...}
public DateTime AddTo(DateTime date){...}
public DateSpan Invert(){...}
public override string ToString(){...}
}
public static DateSpan DateSpanTo(this DateTime min, DateTime max)
public static DateTime Add(this DateTime date, DateSpan dateSpan)
Example:
var cer = new DateTime(1547, 9, 29);
var sha = new DateTime(1564, 4, 26);
DateSpan ds1 = cer.DateSpanTo(sha); // 16 Years, 6 Months, 28 Days
DateSpan ds2 = sha.DateSpanTo(cer); // -16 Years, -6 Months, -28 Days
var sha2= cer.Add(ds1);
Console.WriteLine(sha == sha2); //true
var cer2 = sha.Add(ds2);
Console.WriteLine(cer == cer2); //true
Note: DateSpan.ToString has year(s), month(s) and day(s) correctly pluralized, and avoids writing them when they are zero
new DateSpan(0, 1, 0).ToString(); // returns: "1 Month"
Helper methods to compare dates and get the Min or Max.
public static DateTime Min(this DateTime a, DateTime b)
public static DateTime Max(this DateTime a, DateTime b)
public static DateTime Min(this DateTime a, DateTime? b)
public static DateTime Max(this DateTime a, DateTime? b)
public static DateTime? Min(this DateTime? a, DateTime? b)
public static DateTime? Max(this DateTime? a, DateTime? b)
Helper methods to truncate dates
public static DateTime TrimToSeconds(this DateTime dateTime)
public static DateTime TrimToMinutes(this DateTime dateTime)
public static DateTime TrimToHours(this DateTime dateTime)
public static DateTime TrimTo(this DateTime dateTime, DateTimePrecision precision)
public static DateTimePrecision GetPrecision(this DateTime dateTime)
With
public enum DateTimePrecision
{
Days,
Hours,
Minutes,
Seconds,
Milliseconds,
}
Returns a human-friendly representation of two date relative to another day (tipically today)
public static string SmartDatePattern(this DateTime date)
public static string SmartDatePattern(this DateTime date, DateTime currentdate)
Possible return values are "Today", "Yesterday", "Last Monday", "May 10th" or "May 10th, 1996", etc.. all localized in the current culture.
Also returns human-friendly dates, but without taking week days into account and with higher precision (up to seconds).
public static string ToAgoString(this DateTime dateTime)
public static string ToAgoString(this DateTime dateTime, DateTime now)
Possible return values are "1 day ago", "In 3 months", "In 0 seconds", etc..
Returns the date of the first day of the month. Useful to differentiate November 2014 from November 2013. This method can be translated to SQL.
public static DateTime MonthStart(this DateTime dateTime)
Returns the week number of a date, using CalendarWeekRule.FirstDay
and the current culture DateTimeFormat.FirstDayOfWeek
.
This method can be translated to SQL.
public static int WeekNumber(this DateTime dateTime)
© Signum Software. All Rights Reserved.
Powered by Signum Framework