Table of Contents

Class DateTimeExtensions

Namespace
Cuemon.Extensions
Assembly
Cuemon.Extensions.Core.dll

Extension methods for the DateTime struct.

public static class DateTimeExtensions
Inheritance
DateTimeExtensions

Examples

DateTimeExtensions provides extension methods for DateTime including rounding, range checking, time-of-day classification, kind conversion, and Unix epoch round-tripping. This example creates UTC and local DateTime values and applies Floor to snap to the nearest hour, Ceiling to round up to the next hour, IsWithinRange for containment checks, and classification methods like IsTimeOfDayMorning and IsTimeOfDayEvening. It also converts between DateTimeKind values using ToUtcKind, ToLocalKind, and ToDefaultKind, and round-trips through Unix epoch seconds via ToUnixEpochTime and FromUnixEpochTime. Console output confirms each result, including the snapped timestamp, boolean range check, time-of-day flags, updated Kind values, and the restored timestamp.

using System;
using Cuemon;
using Cuemon.Extensions;

namespace MyApp.Examples;

public static class DateTimeExtensionsExample
{
    public static void Demonstrate()
    {
        var utc = new DateTime(2025, 6, 16, 14, 33, 22, DateTimeKind.Utc);
        var local = new DateTime(2025, 6, 16, 14, 33, 22, DateTimeKind.Local);

        var floorHour = utc.Floor(TimeSpan.FromHours(1));
        var ceilingHour = utc.Ceiling(TimeSpan.FromHours(1));
        var floorFifteenMinutes = utc.Floor(15, TimeUnit.Minutes);
        var roundedUp = utc.Round(TimeSpan.FromMinutes(30), VerticalDirection.Up);
        var isWithinRange = utc.IsWithinRange(new DateTimeRange(utc.AddHours(-1), utc.AddHours(1)));

        var isNight = new DateTime(2025, 6, 16, 22, 15, 0, DateTimeKind.Utc).IsTimeOfDayNight();
        var isMorning = new DateTime(2025, 6, 16, 6, 15, 0, DateTimeKind.Utc).IsTimeOfDayMorning();
        var isForenoon = new DateTime(2025, 6, 16, 10, 15, 0, DateTimeKind.Utc).IsTimeOfDayForenoon();
        var isAfternoon = utc.IsTimeOfDayAfternoon();
        var isEvening = new DateTime(2025, 6, 16, 19, 15, 0, DateTimeKind.Utc).IsTimeOfDayEvening();

        var utcKind = local.ToUtcKind();
        var localKind = utc.ToLocalKind();
        var unspecifiedKind = utc.ToDefaultKind();

        var unixTime = utc.ToUnixEpochTime();
        var restored = unixTime.FromUnixEpochTime();

        Console.WriteLine(floorHour.ToString("O"));
        Console.WriteLine(ceilingHour.ToString("O"));
        Console.WriteLine(floorFifteenMinutes.ToString("O"));
        Console.WriteLine(roundedUp.ToString("O"));
        Console.WriteLine(isWithinRange);
        Console.WriteLine($"{isNight}, {isMorning}, {isForenoon}, {isAfternoon}, {isEvening}");
        Console.WriteLine($"{utcKind.Kind}, {localKind.Kind}, {unspecifiedKind.Kind}");
        Console.WriteLine(restored.ToString("O"));
    }
}

Methods

Ceiling(DateTime, double, TimeUnit)

Returns a DateTime value that is rounded towards positive infinity.

public static DateTime Ceiling(this DateTime value, double interval, TimeUnit timeUnit)

Parameters

value DateTime

The DateTime to extend.

interval double

The double value that in combination with timeUnit specifies the rounding of value.

timeUnit TimeUnit

One of the enumeration values that specifies the time unit of interval.

Returns

DateTime

A DateTime value that is rounded towards positive infinity.

Exceptions

ArgumentOutOfRangeException

interval is 0.

Ceiling(DateTime, TimeSpan)

Returns a DateTime value that is rounded towards positive infinity.

public static DateTime Ceiling(this DateTime value, TimeSpan interval)

Parameters

value DateTime

The DateTime to extend.

interval TimeSpan

The TimeSpan value that specifies the rounding of value.

Returns

DateTime

A DateTime value that is rounded towards positive infinity.

Floor(DateTime, double, TimeUnit)

Returns a DateTime value that is rounded towards negative infinity.

public static DateTime Floor(this DateTime value, double interval, TimeUnit timeUnit)

Parameters

value DateTime

The DateTime to extend.

interval double

The double value that in combination with timeUnit specifies the rounding of value.

timeUnit TimeUnit

One of the enumeration values that specifies the time unit of interval.

Returns

DateTime

A DateTime value that is rounded towards negative infinity.

Exceptions

ArgumentOutOfRangeException

interval is 0.

Floor(DateTime, TimeSpan)

Returns a DateTime value that is rounded towards negative infinity.

public static DateTime Floor(this DateTime value, TimeSpan interval)

Parameters

value DateTime

The DateTime to extend.

interval TimeSpan

The TimeSpan value that specifies the rounding of value.

Returns

DateTime

A DateTime value that is rounded towards negative infinity.

IsTimeOfDayAfternoon(DateTime)

Determines whether the specified value is within Afternoon.

public static bool IsTimeOfDayAfternoon(this DateTime value)

Parameters

value DateTime

The DateTime to extend.

Returns

bool

true if value is within Afternoon; otherwise false.

IsTimeOfDayEvening(DateTime)

Determines whether the specified value is within Evening.

public static bool IsTimeOfDayEvening(this DateTime value)

Parameters

value DateTime

The DateTime to extend.

Returns

bool

true if value is within Evening; otherwise false.

IsTimeOfDayForenoon(DateTime)

Determines whether the specified value is within Forenoon.

public static bool IsTimeOfDayForenoon(this DateTime value)

Parameters

value DateTime

The DateTime to extend.

Returns

bool

true if value is within Forenoon; otherwise false.

IsTimeOfDayMorning(DateTime)

Determines whether the specified value is within Morning.

public static bool IsTimeOfDayMorning(this DateTime value)

Parameters

value DateTime

The DateTime to extend.

Returns

bool

true if value is within Morning; otherwise false.

IsTimeOfDayNight(DateTime)

Determines whether the specified value is within Night.

public static bool IsTimeOfDayNight(this DateTime value)

Parameters

value DateTime

The DateTime to extend.

Returns

bool

true if value is within Night; otherwise false.

IsWithinRange(DateTime, DateTimeRange)

Determines whether the specified value is within range.

public static bool IsWithinRange(this DateTime value, DateTimeRange range)

Parameters

value DateTime

The DateTime to extend.

range DateTimeRange

The DateTimeRange of value.

Returns

bool

true if value is within the specified range; otherwise false.

IsWithinRange(DateTime, DateTime, DateTime)

Determines whether the specified value is within range of min and max.

public static bool IsWithinRange(this DateTime value, DateTime min, DateTime max)

Parameters

value DateTime

The DateTime to extend.

min DateTime

The minimum value of value.

max DateTime

The maximum value of value.

Returns

bool

true if value is within the specified range of min and max; otherwise false.

Round(DateTime, double, TimeUnit, VerticalDirection)

Returns a DateTime value that is rounded either towards negative infinity or positive infinity.

public static DateTime Round(this DateTime value, double interval, TimeUnit timeUnit, VerticalDirection direction)

Parameters

value DateTime

The DateTime to extend.

interval double

The double value that in combination with timeUnit specifies the rounding of value.

timeUnit TimeUnit

One of the enumeration values that specifies the time unit of interval.

direction VerticalDirection

One of the enumeration values that specifies the direction of the rounding.

Returns

DateTime

A DateTime value that is rounded either towards negative infinity or positive infinity.

Exceptions

ArgumentOutOfRangeException

direction is an invalid enumeration value.

ArgumentOutOfRangeException

interval is Zero.

Round(DateTime, TimeSpan, VerticalDirection)

Returns a DateTime value that is rounded either towards negative infinity or positive infinity.

public static DateTime Round(this DateTime value, TimeSpan interval, VerticalDirection direction)

Parameters

value DateTime

The DateTime to extend.

interval TimeSpan

The TimeSpan value that specifies the rounding of value.

direction VerticalDirection

One of the enumeration values that specifies the direction of the rounding.

Returns

DateTime

A DateTime value that is rounded either towards negative infinity or positive infinity.

Exceptions

ArgumentOutOfRangeException

direction is an invalid enumeration value.

ArgumentOutOfRangeException

interval is Zero.

ToDefaultKind(DateTime)

Converts the specified value to a representation that is not specified as either local time or UTC.

public static DateTime ToDefaultKind(this DateTime value)

Parameters

value DateTime

The DateTime to extend.

Returns

DateTime

A new DateTime value initialized to Unspecified that has the same number of ticks as the object represented by the value parameter.

ToLocalKind(DateTime)

Converts the specified value to a local time representation.

public static DateTime ToLocalKind(this DateTime value)

Parameters

value DateTime

The DateTime to extend.

Returns

DateTime

A new DateTime value initialized to Local that has the same number of ticks as the object represented by the value parameter.

ToUnixEpochTime(DateTime)

Converts the specified value to an equivalent UNIX Epoch time representation.

public static double ToUnixEpochTime(this DateTime value)

Parameters

value DateTime

The DateTime value to extend.

Returns

double

A double value that is equivalent to value.

Remarks

This implementation converts the value to an UTC representation ONLY if the Kind equals Local.

ToUtcKind(DateTime)

Converts the specified value to a Coordinated Universal Time (UTC) representation.

public static DateTime ToUtcKind(this DateTime value)

Parameters

value DateTime

The DateTime to extend.

Returns

DateTime

A new DateTime value initialized to Utc that has the same number of ticks as the object represented by the value parameter.