Table of Contents

Struct DateTime64

Namespace
NumSharp
Assembly
NumSharp.dll

A 64-bit signed tick count representing a date/time value with full long range and a NaT sentinel, matching NumPy's np.datetime64 semantics. Used as a conversion-helper type in Converts.

[Serializable]
public readonly struct DateTime64 : IComparable, IComparable<DateTime64>, IEquatable<DateTime64>, IConvertible, ISpanFormattable, IFormattable
Implements
Inherited Members
Extension Methods

Remarks

One tick equals 100 nanoseconds (same unit as Ticks). Ticks == 0 is midnight of 1 January 0001 (the Gregorian epoch of DateTime), which is not the Unix epoch.

NaT semantics. NaT (Ticks == long.MinValue) is Not-a-Time, analogous to IEEE 754 NaN:

  • NaT propagates through arithmetic.
  • operator == / != / < / > / <= / >= follow NumPy: any comparison involving NaT is false for ==/</>/<=/>=, and true for !=.
  • Equals(DateTime64) follows the IEquatable<T> convention (two NaTs are considered equal bit-wise) so that GetHashCode() is contract-compliant and NaT can be used as a Dictionary<TKey, TValue> key. This mirrors how .NET handles NaN: double.NaN.Equals(double.NaN) is true but double.NaN == double.NaN is false.

Constructors

DateTime64(DateTime)

Constructs a DateTime64 from a DateTime. The Kind is discarded (NumPy datetime64 has no timezone).

public DateTime64(DateTime dateTime)

Parameters

dateTime DateTime

DateTime64(DateTimeOffset)

Constructs a DateTime64 from a DateTimeOffset. Stored as UtcTicks (offset discarded).

public DateTime64(DateTimeOffset dateTimeOffset)

Parameters

dateTimeOffset DateTimeOffset

DateTime64(long)

Constructs a DateTime64 from a raw tick count (any int64, including NaT).

public DateTime64(long ticks)

Parameters

ticks long

Fields

Epoch

The .NET calendar epoch (midnight 0001-01-01), same as MinValue.

public static readonly DateTime64 Epoch

Field Value

DateTime64

MaxValue

The largest representable value (Ticks == long.MaxValue).

public static readonly DateTime64 MaxValue

Field Value

DateTime64

MinValue

The smallest non-NaT representable value (Ticks == long.MinValue + 1).

public static readonly DateTime64 MinValue

Field Value

DateTime64

NaT

Not-a-Time sentinel (Ticks == long.MinValue), matching NumPy.

public static readonly DateTime64 NaT

Field Value

DateTime64

Properties

IsNaT

true iff this instance is Not-a-Time (Ticks == long.MinValue).

public bool IsNaT { get; }

Property Value

bool

IsValidDateTime

true iff Ticks is inside the legal range of DateTime, i.e. [0, DateTime.MaxValue.Ticks].

public bool IsValidDateTime { get; }

Property Value

bool

Ticks

The raw 100-ns tick count (full int64; long.MinValue for NaT).

public long Ticks { get; }

Property Value

long

Methods

Add(TimeSpan)

Add a TimeSpan. NaT propagates; overflow saturates to NaT.

public DateTime64 Add(TimeSpan value)

Parameters

value TimeSpan

Returns

DateTime64

AddTicks(long)

Add a raw tick delta. NaT propagates; overflow saturates to NaT.

public DateTime64 AddTicks(long delta)

Parameters

delta long

Returns

DateTime64

Compare(DateTime64, DateTime64)

Compares by ticks. NaT sorts before every other value (as the smallest int64).

public static int Compare(DateTime64 t1, DateTime64 t2)

Parameters

t1 DateTime64
t2 DateTime64

Returns

int

CompareTo(DateTime64)

Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.

public int CompareTo(DateTime64 value)

Parameters

value DateTime64

Returns

int

A value that indicates the relative order of the objects being compared. The return value has these meanings:

Value Meaning
Less than zero This instance precedes other in the sort order.
Zero This instance occurs in the same position in the sort order as other.
Greater than zero This instance follows other in the sort order.

CompareTo(object?)

Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.

public int CompareTo(object? value)

Parameters

value object

Returns

int

A value that indicates the relative order of the objects being compared. The return value has these meanings:

Value Meaning
Less than zero This instance precedes obj in the sort order.
Zero This instance occurs in the same position in the sort order as obj.
Greater than zero This instance follows obj in the sort order.

Exceptions

ArgumentException

obj is not the same type as this instance.

Equals(DateTime64)

Bitwise tick equality (NaT.Equals(NaT) returns true).

public bool Equals(DateTime64 other)

Parameters

other DateTime64

Returns

bool

Equals(DateTime64, DateTime64)

public static bool Equals(DateTime64 t1, DateTime64 t2)

Parameters

t1 DateTime64
t2 DateTime64

Returns

bool

Equals(object?)

Indicates whether this instance and a specified object are equal.

public override bool Equals(object? value)

Parameters

value object

Returns

bool

true if obj and this instance are the same type and represent the same value; otherwise, false.

GetHashCode()

Returns the hash code for this instance.

public override int GetHashCode()

Returns

int

A 32-bit signed integer that is the hash code for this instance.

Parse(string)

Parses a string produced by ToString(). Case-sensitive "NaT" literal returns NaT. Otherwise delegates to Parse(string, IFormatProvider).

public static DateTime64 Parse(string s)

Parameters

s string

Returns

DateTime64

Parse(string, IFormatProvider?)

public static DateTime64 Parse(string s, IFormatProvider? provider)

Parameters

s string
provider IFormatProvider

Returns

DateTime64

Subtract(DateTime64)

Difference as a TimeSpan. If either operand is NaT, returns MinValue (TimeSpan's NaT-equivalent, since TimeSpan.MinValue.Ticks == long.MinValue).

public TimeSpan Subtract(DateTime64 other)

Parameters

other DateTime64

Returns

TimeSpan

Subtract(TimeSpan)

Subtract a TimeSpan. NaT propagates; overflow saturates to NaT.

public DateTime64 Subtract(TimeSpan value)

Parameters

value TimeSpan

Returns

DateTime64

ToDateTime()

Convert to DateTime. Throws InvalidOperationException for NaT / out-of-range.

public DateTime ToDateTime()

Returns

DateTime

ToDateTime(DateTime)

Convert to DateTime, returning fallback for NaT / out-of-range values instead of throwing.

public DateTime ToDateTime(DateTime fallback)

Parameters

fallback DateTime

Returns

DateTime

ToDateTimeOffset()

Convert to DateTimeOffset at UTC. Throws for NaT / out-of-range.

public DateTimeOffset ToDateTimeOffset()

Returns

DateTimeOffset

ToDateTimeOffset(DateTimeOffset)

Convert to DateTimeOffset, returning fallback for NaT / out-of-range values instead of throwing.

public DateTimeOffset ToDateTimeOffset(DateTimeOffset fallback)

Parameters

fallback DateTimeOffset

Returns

DateTimeOffset

ToString()

Formats as ISO-8601 (DateTime's "o" format) for in-range values, "NaT" for NaT, and "DateTime64(ticks=N)" for values outside DateTime's range.

public override string ToString()

Returns

string

ToString(IFormatProvider?)

public string ToString(IFormatProvider? provider)

Parameters

provider IFormatProvider

Returns

string

ToString(string?)

public string ToString(string? format)

Parameters

format string

Returns

string

ToString(string?, IFormatProvider?)

Formats the value of the current instance using the specified format.

public string ToString(string? format, IFormatProvider? provider)

Parameters

format string

The format to use.

-or-

A null reference (Nothing in Visual Basic) to use the default format defined for the type of the IFormattable implementation.

provider IFormatProvider

Returns

string

The value of the current instance in the specified format.

TryFormat(Span<char>, out int, ReadOnlySpan<char>, IFormatProvider?)

Non-allocating formatter: writes directly to destination when possible via TryFormat(Span<char>, out int, ReadOnlySpan<char>, IFormatProvider).

public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default, IFormatProvider? provider = null)

Parameters

destination Span<char>
charsWritten int
format ReadOnlySpan<char>
provider IFormatProvider

Returns

bool

TryParse(string?, out DateTime64)

public static bool TryParse(string? s, out DateTime64 result)

Parameters

s string
result DateTime64

Returns

bool

TryParse(string?, IFormatProvider?, DateTimeStyles, out DateTime64)

public static bool TryParse(string? s, IFormatProvider? provider, DateTimeStyles styles, out DateTime64 result)

Parameters

s string
provider IFormatProvider
styles DateTimeStyles
result DateTime64

Returns

bool

TryToDateTime(out DateTime)

Try to convert to DateTime. Returns false for NaT / out-of-range values (result set to MinValue).

public bool TryToDateTime(out DateTime result)

Parameters

result DateTime

Returns

bool

TryToDateTimeOffset(out DateTimeOffset)

Try to convert to DateTimeOffset.

public bool TryToDateTimeOffset(out DateTimeOffset result)

Parameters

result DateTimeOffset

Returns

bool

Operators

operator +(DateTime64, TimeSpan)

public static DateTime64 operator +(DateTime64 d, TimeSpan t)

Parameters

d DateTime64
t TimeSpan

Returns

DateTime64

operator ==(DateTime64, DateTime64)

public static bool operator ==(DateTime64 d1, DateTime64 d2)

Parameters

d1 DateTime64
d2 DateTime64

Returns

bool

explicit operator DateTime(DateTime64)

Explicit narrowing to DateTime. Throws for NaT / out-of-range.

public static explicit operator DateTime(DateTime64 value)

Parameters

value DateTime64

Returns

DateTime

explicit operator DateTimeOffset(DateTime64)

Explicit narrowing to DateTimeOffset (UTC). Throws for NaT / out-of-range.

public static explicit operator DateTimeOffset(DateTime64 value)

Parameters

value DateTime64

Returns

DateTimeOffset

explicit operator long(DateTime64)

Explicit extraction of the raw int64 tick count.

public static explicit operator long(DateTime64 value)

Parameters

value DateTime64

Returns

long

operator >(DateTime64, DateTime64)

public static bool operator >(DateTime64 d1, DateTime64 d2)

Parameters

d1 DateTime64
d2 DateTime64

Returns

bool

operator >=(DateTime64, DateTime64)

public static bool operator >=(DateTime64 d1, DateTime64 d2)

Parameters

d1 DateTime64
d2 DateTime64

Returns

bool

implicit operator DateTime64(DateTime)

Implicit widening from DateTime (drops Kind).

public static implicit operator DateTime64(DateTime value)

Parameters

value DateTime

Returns

DateTime64

implicit operator DateTime64(DateTimeOffset)

Implicit widening from DateTimeOffset (via UtcTicks).

public static implicit operator DateTime64(DateTimeOffset value)

Parameters

value DateTimeOffset

Returns

DateTime64

implicit operator DateTime64(long)

Implicit widening from long (raw tick count).

public static implicit operator DateTime64(long ticks)

Parameters

ticks long

Returns

DateTime64

operator !=(DateTime64, DateTime64)

public static bool operator !=(DateTime64 d1, DateTime64 d2)

Parameters

d1 DateTime64
d2 DateTime64

Returns

bool

operator <(DateTime64, DateTime64)

public static bool operator <(DateTime64 d1, DateTime64 d2)

Parameters

d1 DateTime64
d2 DateTime64

Returns

bool

operator <=(DateTime64, DateTime64)

public static bool operator <=(DateTime64 d1, DateTime64 d2)

Parameters

d1 DateTime64
d2 DateTime64

Returns

bool

operator -(DateTime64, DateTime64)

public static TimeSpan operator -(DateTime64 d1, DateTime64 d2)

Parameters

d1 DateTime64
d2 DateTime64

Returns

TimeSpan

operator -(DateTime64, TimeSpan)

public static DateTime64 operator -(DateTime64 d, TimeSpan t)

Parameters

d DateTime64
t TimeSpan

Returns

DateTime64