DateTime Extensions - SaintOswald/SaintOswald.Libraries.Extensions GitHub Wiki

Home > DateTime Extensions

Requirements

Make the DateTimeExtensions library available for use:

using SaintOswald.Libraries.Extensions.DateTimeExtensions;

Extensions


IsAnniversaryOf(dateTime, comparison)

Checks if the specified DateTime value is the anniversary (i.e. the same month and day but at least one year later) of the comparison date and time

Name Description
dateTime System.DateTime
The DateTime value to check
comparison System.DateTime
The comparison to check if the specified DateTime value is the anniversary of

Returns

True if the specified DateTime value is the anniversary of the given comparison, otherwise returns false

Sample Usage

[TestMethod]
public void TestIsAnniversaryOf()
{
    Assert.IsTrue(new DateTime(2016, 1, 11).IsAnniversaryOf(new DateTime(2015, 1, 11)));
    Assert.IsTrue(new DateTime(2016, 1, 11).IsAnniversaryOf(new DateTime(2014, 1, 11)));
    Assert.IsFalse(new DateTime(2016, 1, 12).IsAnniversaryOf(new DateTime(2015, 1, 11)));
}

IsBetween(dateTime, startComparison, endComparison)

Checks if the specified DateTime value is between the start and end comparison date and times

Name Description
dateTime System.DateTime
The DateTime value to check
startComparison System.DateTime
The start comparison to check if the specified DateTime value is between
endComparison System.DateTime
The end comparison to check if the specified DateTime value is between

Returns

True if the specified DateTime value is between the given start and end comparisons, otherwise returns false

Sample Usage

[TestMethod]
public void TestIsBetween()
{
    Assert.IsTrue(new DateTime(2016, 1, 7).IsBetween(new DateTime(2016, 1, 1), new DateTime(2016, 12, 31)));
    Assert.IsFalse(new DateTime(2016, 1, 7).IsBetween(new DateTime(2015, 1, 1), new DateTime(2015, 12, 31)));
}

IsEarlierThan(dateTime, comparison)

Checks if the specified DateTime value is earlier than the given comparison date and time

Name Description
dateTime System.DateTime
The DateTime value to check
comparison System.DateTime
The comparison to check if the specified DateTime value is earlier than

Returns

True if the specified DateTime value is earlier than the given comparison, otherwise returns false

Sample Usage

[TestMethod]
public void TestIsEarlierThan()
{
    Assert.IsTrue(new DateTime(2016, 1, 6).IsEarlierThan(new DateTime(2016, 1, 7)));
    Assert.IsFalse(new DateTime(2016, 1, 7).IsEarlierThan(new DateTime(2016, 1, 6)));
}

IsInFuture(dateTime)

Checks if the specified DateTime value is in the future

Name Description
dateTime System.DateTime
The DateTime value to check

Returns

True if the specified DateTime value is in the future, otherwise returns false

Sample Usage

[TestMethod]
public void TestIsInFuture()
{
    Assert.IsTrue(DateTime.Now.AddDays(1).IsInFuture());
    Assert.IsTrue(DateTime.Now.AddSeconds(10).IsInFuture());
    Assert.IsFalse(DateTime.Now.AddDays(-1).IsInFuture());
    Assert.IsFalse(DateTime.Now.AddSeconds(-1).IsInFuture());
}

IsInPast(dateTime)

Checks if the specified DateTime value is in the past

Name Description
dateTime System.DateTime
The DateTime value to check

Returns

True if the specified DateTime value is in the past, otherwise returns false

Sample Usage

[TestMethod]
public void TestIsInPast()
{
    Assert.IsTrue(DateTime.Now.AddDays(-1).IsInPast());
    Assert.IsTrue(DateTime.Now.AddSeconds(-10).IsInPast());
    Assert.IsFalse(DateTime.Now.AddDays(1).IsInPast());
    Assert.IsFalse(DateTime.Now.AddSeconds(1).IsInPast());
}

IsLaterThan(dateTime, comparison)

Checks if the specified DateTime value is later than the given comparison date and time

Name Description
dateTime System.DateTime
The DateTime value to check
comparison System.DateTime
The comparison to check if the specified DateTime value is later than

Returns

True if the specified DateTime value is later than the given comparison, otherwise returns false

Sample Usage

[TestMethod]
public void TestIsLaterThan()
{
    Assert.IsTrue(new DateTime(2016, 1, 6).IsLaterThan(new DateTime(2016, 1, 5)));
    Assert.IsFalse(new DateTime(2016, 1, 5).IsLaterThan(new DateTime(2016, 1, 6)));
}

IsSameAs(dateTime, comparison)

Checks if the specified DateTime value is the same as the given comparison date and time

Name Description
dateTime System.DateTime
The DateTime value to check
comparison System.DateTime
The comparison to check if the specified DateTime value is the same as

Returns

True if the specified DateTime value is the same as the given comparison, otherwise returns false

Sample Usage

[TestMethod]
public void TestIsSameAs()
{
    Assert.IsTrue(new DateTime(2016, 1, 6).IsSameAs(new DateTime(2016, 1, 6)));
    Assert.IsFalse(new DateTime(2016, 1, 6).IsSameAs(new DateTime(2016, 1, 7)));
}

IsSameDateAs(dateTime, comparison)

Checks if the specified DateTime value is the same date (i.e. year, month and day) as the given comparison date and time

Name Description
dateTime System.DateTime
The DateTime value to check
comparison System.DateTime
The comparison to check if the specified DateTime value is the same date as

Returns

True if the specified DateTime value is the same date as the given comparison, otherwise returns false

Sample Usage

[TestMethod]
public void TestIsSameDateAs()
{
    Assert.IsTrue(new DateTime(2016, 1, 6).IsSameDateAs(new DateTime(2016, 1, 6)));
    Assert.IsFalse(new DateTime(2016, 1, 6).IsSameDateAs(new DateTime(2016, 1, 7)));
}

IsSameTimeAs(dateTime, comparison)

Checks if the specified DateTime value is the same time (i.e. hour, minute and second) as the given comparison date and time

Name Description
dateTime System.DateTime
The DateTime value to check
comparison System.DateTime
The comparison to check if the specified DateTime value is the same time as

Returns

True if the specified DateTime value is the same time as the given comparison, otherwise returns false

Sample Usage

[TestMethod]
public void TestIsSameTimeAs()
{
    Assert.IsTrue(new DateTime(2016, 1, 6, 21, 46, 16).IsSameTimeAs(new DateTime(2016, 1, 6, 21, 46, 16)));
    Assert.IsTrue(new DateTime(2016, 1, 5, 21, 46, 16).IsSameTimeAs(new DateTime(2016, 1, 6, 21, 46, 16)));
    Assert.IsFalse(new DateTime(2016, 1, 6, 21, 46, 16).IsSameTimeAs(new DateTime(2016, 1, 7, 22, 46, 16)));
}

IsToday(dateTime)

Checks if the specified DateTime value is today's date

Name Description
dateTime System.DateTime
The DateTime value to check

Returns

True if the specified DateTime value is today's date, otherwise returns false

Sample Usage

[TestMethod]
public void TestIsToday()
{
    Assert.IsTrue(DateTime.Now.IsToday());
    Assert.IsFalse(DateTime.Now.AddDays(-1).IsToday());
    Assert.IsFalse(DateTime.Now.AddDays(1).IsToday());
}

IsTomorrow(dateTime)

Checks if the specified DateTime value is tomorrow's date

Name Description
dateTime System.DateTime
The DateTime value to check

Returns

True if the specified DateTime value is tomorrow's date, otherwise returns false

Sample Usage

[TestMethod]
public void TestIsTomorrow()
{
    Assert.IsTrue(DateTime.Now.AddDays(1).IsTomorrow());
    Assert.IsFalse(DateTime.Now.IsTomorrow());
    Assert.IsFalse(DateTime.Now.AddDays(-1).IsTomorrow());
    Assert.IsFalse(DateTime.Now.AddDays(2).IsTomorrow());
}

IsWeekday(dateTime)

Checks if the specified DateTime value is a weekday (i.e. not a Saturday or Sunday). Note: Method is not culture aware and only defines a weekday as any day that is not a Saturday or Sunday

Name Description
dateTime System.DateTime
The DateTime value to check

Returns

True if the specified DateTime value is a weekday, otherwise returns false

Sample Usage

[TestMethod]
public void TestIsWeekday()
{
    Assert.IsTrue(new DateTime(2016, 1, 4).IsWeekday());   // Monday
    Assert.IsFalse(new DateTime(2016, 1, 3).IsWeekday());  // Sunday
    Assert.IsFalse(new DateTime(2016, 1, 2).IsWeekday());  // Saturday
}

IsWeekend(dateTime)

Checks if the specified DateTime value is a weekend (i.e. a Saturday or Sunday). Note: Method is not culture aware and defines a weekend day as a Saturday or Sunday only

Name Description
dateTime System.DateTime
The DateTime value to check

Returns

True if the specified DateTime value is a weekend, otherwise returns false

Sample Usage

[TestMethod]
public void TestIsWeekend()
{
    Assert.IsTrue(new DateTime(2016, 1, 2).IsWeekend());   // Saturday
    Assert.IsTrue(new DateTime(2016, 1, 3).IsWeekend());   // Sunday
    Assert.IsFalse(new DateTime(2016, 1, 4).IsWeekend());  // Monday
}

IsYesterday(dateTime)

Checks if the specified DateTime value is yesterday's date

Name Description
dateTime System.DateTime
The DateTime value to check

Returns

True if the specified DateTime value is yesterday's date, otherwise returns false

Sample Usage

[TestMethod]
public void TestIsYesterday()
{
    Assert.IsTrue(DateTime.Now.AddDays(-1).IsYesterday());
    Assert.IsFalse(DateTime.Now.IsYesterday());
    Assert.IsFalse(DateTime.Now.AddDays(-2).IsYesterday());
    Assert.IsFalse(DateTime.Now.AddDays(1).IsYesterday());
}

ToCopyright(dateTime, copyrightStartYear)

Returns the copyright for the specified DateTime value based on the given copyright start year. For example, a copyright start year of 2016 and a DateTime value of 2016 will return "2016". For a copyright start year of 2015 and a DateTime value of 2016 "2015 - 2016" will be returned

Name Description
dateTime System.DateTime
The DateTime value to return the copyright for
copyrightStartYear System.Int32
The year in which copyright started

Returns

The copyright for the specified DateTime value

Exceptions

System.ArgumentException: Thrown when the specified copyright start year is later than the given DateTime year

Sample Usage

[TestMethod]
public void TestToCopyright()
{
    Assert.AreEqual("2016", new DateTime(2016, 11, 1).ToCopyright(2016));
    Assert.AreEqual("2015 - 2016", new DateTime(2016, 11, 1).ToCopyright(2015));
}

ToEndOfDay(dateTime)

Returns a new DateTime instance for the DateTime value with the time set to the end of the day (i.e. 23:59:59:0999)

Name Description
dateTime System.DateTime
The DateTime value to return the new DateTime for

Returns

A DateTime instance for the DateTime value with the time set to the end of the day

Sample Usage

[TestMethod]
public void TestToEndOfDay()
{
    Assert.AreEqual(new DateTime(2016, 1, 7, 23, 59, 59, 999), new DateTime(2016, 1, 7, 19, 34, 56, 178).ToEndOfDay());
}

ToFirstDayOfMonth(dateTime)

Returns a new DateTime instance representing the first day of the month for the specified DateTime value

Name Description
dateTime System.DateTime
The DateTime value to return the new DateTime for

Returns

A DateTime instance representing the first day of the month for the specified DateTime value

Sample Usage

[TestMethod]
public void TestToFirstDayOfMonth()
{
    Assert.AreEqual(new DateTime(2016, 1, 1), new DateTime(2016, 1, 7).ToFirstDayOfMonth());
}

ToLastDayOfMonth(dateTime)

Returns a new DateTime instance representing the last day of the month for the specified DateTime value

Name Description
dateTime System.DateTime
The DateTime value to return the new DateTime for

Returns

A DateTime instance representing the last day of the month for the specified DateTime value

Sample Usage

[TestMethod]
public void TestToLastDayOfMonth()
{
    Assert.AreEqual(new DateTime(2016, 1, 31), new DateTime(2016, 1, 7).ToLastDayOfMonth());
    Assert.AreEqual(new DateTime(2016, 4, 30), new DateTime(2016, 4, 7).ToLastDayOfMonth());
}

ToMidday(dateTime)

Returns a new DateTime instance for the DateTime value with the time set to midday (i.e. 12:00:00:0000)

Name Description
dateTime System.DateTime
The DateTime value to return the new DateTime for

Returns

A DateTime instance for the DateTime value with the time set to the end of the day

Sample Usage

[TestMethod]
public void TestToMidday()
{
    Assert.AreEqual(new DateTime(2016, 1, 7, 12, 0, 0, 0), new DateTime(2016, 1, 7, 19, 34, 56, 178).ToMidday());
}

ToNext(dateTime, dayOfWeek)

Returns a new DateTime instance for the DateTime value with the day set to the next occurrence of the specified day of week. If the specified day of week day of week is the same as the given day of week (e.g. Monday and Monday) then the day 7 days from the DateTime value is returned

Name Description
dateTime System.DateTime
The DateTime value to return the new DateTime for
dayOfWeek System.DayOfWeek
The next day of week occurrence to set the DateTime to

Returns

A DateTime instance for the DateTime value with the day set to the next occurrence of the specified day of week

Sample Usage

[TestMethod]
public void TestToNext()
{
    DateTime next = new DateTime(2016, 1, 7).ToNext(DayOfWeek.Friday);

    Assert.AreEqual(DayOfWeek.Friday, next.DayOfWeek);
    Assert.AreEqual(8, next.Day);

    next = next.ToNext(DayOfWeek.Sunday);
    Assert.AreEqual(DayOfWeek.Sunday, next.DayOfWeek);
    Assert.AreEqual(10, next.Day);
}

ToPrevious(dateTime, dayOfWeek)

Returns a new DateTime instance for the DateTime value with the day set to the previous occurrence of the specified day of week. If the specified day of week day of week is the same as the given day of week (e.g. Monday and Monday) then the day 7 days before the DateTime value is returned

Name Description
dateTime System.DateTime
The DateTime value to return the new DateTime for
dayOfWeek System.DayOfWeek
The previous day of week occurrence to set the DateTime to

Returns

A DateTime instance for the DateTime value with the day set to the previous occurrence of the specified day of week

Sample Usage

[TestMethod]
public void TestToPrevious()
{
    DateTime previous = new DateTime(2016, 1, 7).ToPrevious(DayOfWeek.Wednesday);

    Assert.AreEqual(DayOfWeek.Wednesday, previous.DayOfWeek);
    Assert.AreEqual(6, previous.Day);

    previous = previous.ToPrevious(DayOfWeek.Monday);
    Assert.AreEqual(DayOfWeek.Monday, previous.DayOfWeek);
    Assert.AreEqual(4, previous.Day);
}

ToRelativeTime(dateTime)

Returns the relative time from now (such as "A few seconds ago" or "5 minutes from now") for the specified DateTime value. Note: Method is not locale aware and only returns results in English language format

Name Description
dateTime System.DateTime
The DateTime value to return the relative time for

Returns

The relative time for the specified DateTime value

Sample Usage

[TestMethod]
public void TestToRelativeTime()
{
    Assert.AreEqual("A few seconds ago", DateTime.Now.AddSeconds(-1).ToRelativeTime());
    Assert.AreEqual("A few seconds from now", DateTime.Now.AddSeconds(1).ToRelativeTime());

    Assert.AreEqual("A minute ago", DateTime.Now.AddMinutes(-1).ToRelativeTime());
    Assert.AreEqual("A minute from now", DateTime.Now.AddMinutes(1).ToRelativeTime());

    Assert.AreEqual("2 minutes ago", DateTime.Now.AddMinutes(-2).ToRelativeTime());
    Assert.AreEqual("2 minutes from now", DateTime.Now.AddMinutes(2).ToRelativeTime());

    Assert.AreEqual("An hour ago", DateTime.Now.AddHours(-1).ToRelativeTime());
    Assert.AreEqual("An hour from now", DateTime.Now.AddHours(1).ToRelativeTime());

    Assert.AreEqual("2 hours ago", DateTime.Now.AddHours(-2).ToRelativeTime());
    Assert.AreEqual("2 hours from now", DateTime.Now.AddHours(2).ToRelativeTime());

    Assert.AreEqual("A day ago", DateTime.Now.AddDays(-1).ToRelativeTime());
    Assert.AreEqual("A day from now", DateTime.Now.AddDays(1).ToRelativeTime());

    Assert.AreEqual("2 days ago", DateTime.Now.AddDays(-2).ToRelativeTime());
    Assert.AreEqual("2 days from now", DateTime.Now.AddDays(2).ToRelativeTime());

    Assert.AreEqual("A month ago", DateTime.Now.AddDays(-30).ToRelativeTime());
    Assert.AreEqual("A month from now", DateTime.Now.AddDays(30).ToRelativeTime());

    Assert.AreEqual("2 months ago", DateTime.Now.AddDays(-60).ToRelativeTime());
    Assert.AreEqual("2 months from now", DateTime.Now.AddDays(60).ToRelativeTime());

    Assert.AreEqual("A year ago", DateTime.Now.AddDays(-365).ToRelativeTime());
    Assert.AreEqual("A year from now", DateTime.Now.AddDays(365).ToRelativeTime());

    Assert.AreEqual("2 years ago", DateTime.Now.AddDays(-365 * 2).ToRelativeTime());
    Assert.AreEqual("2 years from now", DateTime.Now.AddDays(365 * 2).ToRelativeTime());
}

ToSimpleFormat(dateTime)

Returns a simple formatted representation of the specified DateTime value, such as "Today at HH:mm" if the DateTime value is today's date, "Yesterday at HH:mm" if the DateTime value is yesterday's date, "Wednesday at HH:mm" if the DateTime value day is up to 6 days ago and the day is a Wednesday or "dd/MM/yyyy at HH:mm" if the DateTime was earlier than 6 days ago. Note: Method is not fully locale aware and only returns results in English language format even though dates and times will be returned according to the current locale

Name Description
dateTime System.DateTime
The DateTime value to return a simple formatted representation of

Returns

The simple formatted representation of the specified DateTime value

Sample Usage

[TestMethod]
public void TestToSimpleFormat()
{
    DateTime dateTime = DateTime.Now;
    Assert.AreEqual($"Today at {dateTime.ToShortTimeString()}", dateTime.ToSimpleFormat());

    dateTime = DateTime.Now.AddDays(-1);
    Assert.AreEqual("Yesterday at " + dateTime.ToShortTimeString(), dateTime.ToSimpleFormat());

    dateTime = DateTime.Now.AddDays(-6);
    Assert.AreEqual(dateTime.ToString("dddd") + " at " + dateTime.ToShortTimeString(), dateTime.ToSimpleFormat());

    dateTime = DateTime.Now.AddDays(-7);
    Assert.AreEqual(dateTime.ToShortDateString() + " at " + dateTime.ToShortTimeString(), dateTime.ToSimpleFormat());
}

ToStartOfDay(dateTime)

Returns a new DateTime instance for the DateTime value with the time set to the start of the day (i.e. 00:00:00:0000)

Name Description
dateTime System.DateTime
The DateTime value to return the new DateTime for

Returns

A DateTime instance for the DateTime value with the time set to the start of the day

Sample Usage

[TestMethod]
public void TestToStartOfDay()
{
    Assert.AreEqual(new DateTime(2016, 1, 7, 0, 0, 0, 0), new DateTime(2016, 1, 7, 19, 34, 56, 178).ToStartOfDay());
}

ToTime(dateTime, hour, minute, second, millisecond)

Returns a new DateTime instance for the DateTime value with the time set to the specified hour, minute, second and (optionally) millisecond

Name Description
dateTime System.DateTime
The DateTime value to return the new DateTime for
hour System.Int32
The hour to set the DateTime value to
minute System.Int32
The minute to set the DateTime value to
second System.Int32
The second to set the DateTime value to
millisecond System.Nullable{System.Int32}
The millisecond to set the DateTime value to (optional). If unspecified the millisecond value from the specified DateTime will be used

Returns

A DateTime instance for the DateTime value with the time set to the specified hour, minute, second and (optionally) millisecond

Sample Usage

[TestMethod]
public void TestToTime()
{
    Assert.AreEqual(new DateTime(2016, 1, 7, 19, 20, 21), new DateTime(2016, 1, 7, 1, 2, 3).ToTime(19, 20, 21));
}

⚠️ **GitHub.com Fallback** ⚠️