Table of Contents

Class TimeHelper

Namespace
Ecng.Common
Assembly
Ecng.Common.dll

Provides various helper methods and properties for working with dates, times, and time offsets.

public static class TimeHelper
Inheritance
TimeHelper
Inherited Members

Fields

China

Represents the China Standard Time zone.

public static readonly TimeZoneInfo China

Field Value

TimeZoneInfo

Cst

Represents the Central Standard Time zone.

public static readonly TimeZoneInfo Cst

Field Value

TimeZoneInfo

Day

A TimeSpan of one day.

public static readonly TimeSpan Day

Field Value

TimeSpan

Est

Represents the Eastern Standard Time zone.

public static readonly TimeZoneInfo Est

Field Value

TimeZoneInfo

Fle

Represents the FLE (Helsinki, Kyiv, Riga, Sofia, Tallinn, Vilnius) Standard Time zone.

public static readonly TimeZoneInfo Fle

Field Value

TimeZoneInfo

Gmt

Represents the GMT Standard Time zone.

public static readonly TimeZoneInfo Gmt

Field Value

TimeZoneInfo

GregorianStart

Represents a reference start date (1/1/1970, UTC).

public static readonly DateTime GregorianStart

Field Value

DateTime

Hour

A TimeSpan of one hour.

public static readonly TimeSpan Hour

Field Value

TimeSpan

Korea

Represents the Korea Standard Time zone.

public static readonly TimeZoneInfo Korea

Field Value

TimeZoneInfo

LessOneDay

A TimeSpan that is one tick less than a day.

public static readonly TimeSpan LessOneDay

Field Value

TimeSpan

Minute1

A TimeSpan of one minute.

public static readonly TimeSpan Minute1

Field Value

TimeSpan

Minute10

A TimeSpan of ten minutes.

public static readonly TimeSpan Minute10

Field Value

TimeSpan

Minute15

A TimeSpan of fifteen minutes.

public static readonly TimeSpan Minute15

Field Value

TimeSpan

Minute5

A TimeSpan of five minutes.

public static readonly TimeSpan Minute5

Field Value

TimeSpan

Month

A TimeSpan of one month (30 days).

public static readonly TimeSpan Month

Field Value

TimeSpan

Moscow

Represents the Russian Standard Time zone (Moscow).

public static readonly TimeZoneInfo Moscow

Field Value

TimeZoneInfo

NanosecondsPerTick

Represents the number of nanoseconds in 1 tick.

public const long NanosecondsPerTick = 100

Field Value

long

TicksPerCentury

Represents the number of ticks in 1 century.

public const long TicksPerCentury = 31536000000000000

Field Value

long

TicksPerMicrosecond

Represents the number of ticks in 1 microsecond.

public const long TicksPerMicrosecond = 10

Field Value

long

TicksPerMillenium

Represents the number of ticks in 1 millenium.

public const long TicksPerMillenium = 315360000000000000

Field Value

long

TicksPerMonth

Represents the number of ticks in 1 month.

public const long TicksPerMonth = 25920000000000

Field Value

long

TicksPerNanosecond

Represents the number of ticks in 1 nanosecond.

public const double TicksPerNanosecond = 0.01

Field Value

double

TicksPerWeek

Represents the number of ticks in 1 week.

public const long TicksPerWeek = 6048000000000

Field Value

long

TicksPerYear

Represents the number of ticks in 1 year.

public const long TicksPerYear = 315360000000000

Field Value

long

Tokyo

Represents the Tokyo Standard Time zone.

public static readonly TimeZoneInfo Tokyo

Field Value

TimeZoneInfo

Tunisia

Represents the West Central Africa Standard Time zone (Tunisia).

public static readonly TimeZoneInfo Tunisia

Field Value

TimeZoneInfo

Week

A TimeSpan of one week (7 days).

public static readonly TimeSpan Week

Field Value

TimeSpan

Year

A TimeSpan of one year (365 days).

public static readonly TimeSpan Year

Field Value

TimeSpan

Properties

Now

Gets the current time including the configured offset.

public static DateTime Now { get; }

Property Value

DateTime

NowOffset

Gets or sets the offset applied to the current time when retrieving Now.

public static TimeSpan NowOffset { get; set; }

Property Value

TimeSpan

NowWithOffset

Gets the current time as a DateTimeOffset including the local offset.

public static DateTimeOffset NowWithOffset { get; }

Property Value

DateTimeOffset

TimeZoneOffset

Gets or sets the time zone offset used for calculations.

public static TimeSpan TimeZoneOffset { get; set; }

Property Value

TimeSpan

UnixNowMls

Gets the current Unix time in milliseconds.

public static double UnixNowMls { get; }

Property Value

double

UnixNowS

Gets the current Unix time in seconds.

public static double UnixNowS { get; }

Property Value

double

Methods

AddMicroseconds(DateTime, long)

Adds the specified number of microseconds to a DateTime.

public static DateTime AddMicroseconds(this DateTime dt, long microseconds)

Parameters

dt DateTime
microseconds long

Returns

DateTime

AddMicroseconds(DateTimeOffset, long)

Adds the specified number of microseconds to a DateTimeOffset.

public static DateTimeOffset AddMicroseconds(this DateTimeOffset dto, long microseconds)

Parameters

dto DateTimeOffset
microseconds long

Returns

DateTimeOffset

AddMicroseconds(TimeSpan, long)

Adds the specified number of microseconds to a TimeSpan.

public static TimeSpan AddMicroseconds(this TimeSpan t, long microseconds)

Parameters

t TimeSpan
microseconds long

Returns

TimeSpan

AddNanoseconds(DateTime, long)

Adds the specified number of nanoseconds to a DateTime.

public static DateTime AddNanoseconds(this DateTime dt, long nanoseconds)

Parameters

dt DateTime
nanoseconds long

Returns

DateTime

AddNanoseconds(DateTimeOffset, long)

Adds the specified number of nanoseconds to a DateTimeOffset.

public static DateTimeOffset AddNanoseconds(this DateTimeOffset dto, long nanoseconds)

Parameters

dto DateTimeOffset
nanoseconds long

Returns

DateTimeOffset

AddNanoseconds(TimeSpan, long)

Adds the specified number of nanoseconds to a TimeSpan.

public static TimeSpan AddNanoseconds(this TimeSpan t, long nanoseconds)

Parameters

t TimeSpan
nanoseconds long

Returns

TimeSpan

ApplyChina(DateTime)

Creates a DateTimeOffset from a DateTime in China Standard Time.

public static DateTimeOffset ApplyChina(this DateTime dt)

Parameters

dt DateTime

Returns

DateTimeOffset

ApplyEst(DateTime)

Creates a DateTimeOffset from a DateTime in Eastern Standard Time.

public static DateTimeOffset ApplyEst(this DateTime dt)

Parameters

dt DateTime

Returns

DateTimeOffset

ApplyLocal(DateTime)

Creates a DateTimeOffset from a DateTime using the local time zone.

public static DateTimeOffset ApplyLocal(this DateTime dt)

Parameters

dt DateTime

Returns

DateTimeOffset

ApplyMoscow(DateTime)

Creates a DateTimeOffset from a DateTime in Russian Standard Time (Moscow).

public static DateTimeOffset ApplyMoscow(this DateTime dt)

Parameters

dt DateTime

Returns

DateTimeOffset

ApplyTimeZone(DateTime, TimeSpan)

Creates a DateTimeOffset from a DateTime with the specified offset.

public static DateTimeOffset ApplyTimeZone(this DateTime dt, TimeSpan offset)

Parameters

dt DateTime
offset TimeSpan

Returns

DateTimeOffset

ApplyTimeZone(DateTime, TimeZoneInfo)

Creates a DateTimeOffset from a DateTime in the specified time zone.

public static DateTimeOffset ApplyTimeZone(this DateTime dt, TimeZoneInfo zone)

Parameters

dt DateTime
zone TimeZoneInfo

Returns

DateTimeOffset

ApplyUtc(DateTime)

Creates a DateTimeOffset from a DateTime using UTC.

public static DateTimeOffset ApplyUtc(this DateTime dt)

Parameters

dt DateTime

Returns

DateTimeOffset

ChangeKind(DateTime, DateTimeKind)

Changes the DateTimeKind of a DateTime.

public static DateTime ChangeKind(this DateTime date, DateTimeKind kind = DateTimeKind.Unspecified)

Parameters

date DateTime
kind DateTimeKind

Returns

DateTime

Convert(DateTimeOffset, TimeZoneInfo)

Converts the specified DateTimeOffset to the provided time zone.

public static DateTimeOffset Convert(this DateTimeOffset dto, TimeZoneInfo zone)

Parameters

dto DateTimeOffset

The DateTimeOffset to convert.

zone TimeZoneInfo

The target TimeZoneInfo.

Returns

DateTimeOffset

A DateTimeOffset in the specified time zone.

ConvertToChina(DateTimeOffset)

Converts a DateTimeOffset to China Standard Time zone.

public static DateTimeOffset ConvertToChina(this DateTimeOffset dto)

Parameters

dto DateTimeOffset

Returns

DateTimeOffset

ConvertToEst(DateTimeOffset)

Converts the specified DateTimeOffset to Eastern Standard Time.

public static DateTimeOffset ConvertToEst(this DateTimeOffset dto)

Parameters

dto DateTimeOffset

The DateTimeOffset to convert.

Returns

DateTimeOffset

A new DateTimeOffset in Eastern Standard Time.

ConvertToMoscow(DateTimeOffset)

Converts the specified DateTimeOffset to Moscow Time.

public static DateTimeOffset ConvertToMoscow(this DateTimeOffset dto)

Parameters

dto DateTimeOffset

The DateTimeOffset to convert.

Returns

DateTimeOffset

A new DateTimeOffset in Moscow Time.

ConvertToUtc(DateTimeOffset)

Converts the specified DateTimeOffset to UTC.

public static DateTimeOffset ConvertToUtc(this DateTimeOffset dto)

Parameters

dto DateTimeOffset

The DateTimeOffset to convert.

Returns

DateTimeOffset

A new DateTimeOffset in UTC.

DaysInMonth(DateTime)

Gets the number of days in the month of the given DateTime.

public static int DaysInMonth(this DateTime date)

Parameters

date DateTime

Returns

int

EndOfDay(DateTime)

Gets the end of the day for the specified DateTime, just before midnight.

public static DateTime EndOfDay(this DateTime dt)

Parameters

dt DateTime

Returns

DateTime

EndOfDay(DateTimeOffset)

Gets the end of the day for the specified DateTimeOffset, just before midnight.

public static DateTimeOffset EndOfDay(this DateTimeOffset dto)

Parameters

dto DateTimeOffset

Returns

DateTimeOffset

FromDateTime(DateTime, string, CultureInfo)

Formats a DateTime to a string using the provided format.

public static string FromDateTime(this DateTime dt, string format, CultureInfo ci = null)

Parameters

dt DateTime
format string
ci CultureInfo

Returns

string

FromDateTimeOffset(DateTimeOffset, string, CultureInfo)

Formats a DateTimeOffset to a string using the provided format.

public static string FromDateTimeOffset(this DateTimeOffset dto, string format, CultureInfo ci = null)

Parameters

dto DateTimeOffset
format string
ci CultureInfo

Returns

string

FromIso8601(string, IFormatProvider)

Parses the specified string as an ISO8601 date/time.

public static DateTime FromIso8601(this string str, IFormatProvider provider = null)

Parameters

str string

The string to parse.

provider IFormatProvider

An optional IFormatProvider.

Returns

DateTime

A DateTime parsed from the string, in UTC.

FromTimeSpan(TimeSpan, string, CultureInfo)

Formats a TimeSpan to a string using the provided format.

public static string FromTimeSpan(this TimeSpan ts, string format, CultureInfo ci = null)

Parameters

ts TimeSpan
format string
ci CultureInfo

Returns

string

FromUnix(double, bool)

Creates a DateTime from a Unix time in seconds or milliseconds.

public static DateTime FromUnix(this double time, bool isSeconds = true)

Parameters

time double

The Unix time to convert.

isSeconds bool

If set to true, interprets time as seconds; otherwise milliseconds.

Returns

DateTime

A DateTime in UTC.

FromUnix(long, bool)

Creates a DateTime from a Unix time in seconds or milliseconds.

public static DateTime FromUnix(this long time, bool isSeconds = true)

Parameters

time long

The Unix time to convert.

isSeconds bool

If set to true, interprets time as seconds; otherwise milliseconds.

Returns

DateTime

A DateTime in UTC.

FromUnixMcs(double)

Creates a DateTime from Microseconds since GregorianStart.

public static DateTime FromUnixMcs(this double mcs)

Parameters

mcs double

Microseconds to convert.

Returns

DateTime

A DateTime in UTC.

FromUnixMcs(long)

Creates a DateTime from Microseconds since GregorianStart.

public static DateTime FromUnixMcs(this long mcs)

Parameters

mcs long

Microseconds to convert.

Returns

DateTime

A DateTime in UTC.

GetIso8601WeekOfYear(DateTime, CultureInfo)

Calculates the ISO8601 week number of the specified date.

public static int GetIso8601WeekOfYear(this DateTime time, CultureInfo ci = null)

Parameters

time DateTime

The DateTime to evaluate.

ci CultureInfo

An optional CultureInfo.

Returns

int

The ISO8601 week of the year.

GetLunarPhase(DateTime)

Determines the lunar phase for the specified date.

public static LunarPhases GetLunarPhase(this DateTime date)

Parameters

date DateTime

The DateTime for which to determine the phase.

Returns

LunarPhases

A LunarPhases value representing the phase of the moon.

GetMicroseconds(DateTime)

Gets the microseconds component from a DateTime.

public static int GetMicroseconds(this DateTime dt)

Parameters

dt DateTime

Returns

int

GetMicroseconds(TimeSpan)

Gets the microseconds component from a TimeSpan.

public static int GetMicroseconds(this TimeSpan ts)

Parameters

ts TimeSpan

Returns

int

GetNanoseconds(DateTime)

Gets the nanoseconds component from a DateTime.

public static int GetNanoseconds(this DateTime dt)

Parameters

dt DateTime

Returns

int

GetNanoseconds(long)

Gets the nanoseconds component from the specified number of ticks.

public static int GetNanoseconds(this long ticks)

Parameters

ticks long

Returns

int

GetNanoseconds(TimeSpan)

Gets the nanoseconds component from a TimeSpan.

public static int GetNanoseconds(this TimeSpan ts)

Parameters

ts TimeSpan

Returns

int

GetUnixDiff(DateTime)

Gets the difference between the specified DateTime and the GregorianStart.

public static TimeSpan GetUnixDiff(this DateTime time)

Parameters

time DateTime

The DateTime to evaluate.

Returns

TimeSpan

A TimeSpan representing the difference.

Exceptions

ArgumentOutOfRangeException

Thrown if the time is earlier than GregorianStart.

IsDateOrTime(Type)

Checks if the given Type is a date or time type.

public static bool IsDateOrTime(this Type type)

Parameters

type Type

The Type to check.

Returns

bool

True if the type is a date/time or TimeSpan; otherwise false.

IsDateTime(Type)

Checks if the given Type is a date/time type.

public static bool IsDateTime(this Type type)

Parameters

type Type

The Type to check.

Returns

bool

True if the type is a DateTime or DateTimeOffset; otherwise false.

Exceptions

ArgumentNullException

Thrown if type is null.

IsWeekday(DateTime)

Checks if the given DateTime falls on a weekday (Monday through Friday).

public static bool IsWeekday(this DateTime date)

Parameters

date DateTime

The DateTime to check.

Returns

bool

True if it is a weekday; otherwise false.

IsWeekday(DateTimeOffset)

Checks if the given DateTimeOffset falls on a weekday (Monday through Friday).

public static bool IsWeekday(this DateTimeOffset date)

Parameters

date DateTimeOffset

The DateTimeOffset to check.

Returns

bool

True if it is a weekday; otherwise false.

IsWeekday(DayOfWeek)

Checks if the given DayOfWeek represents a weekday (Monday through Friday).

public static bool IsWeekday(this DayOfWeek dow)

Parameters

dow DayOfWeek

The DayOfWeek.

Returns

bool

True if it is a weekday; otherwise false.

IsWeekend(DateTime)

Checks if the given DateTime falls on a weekend (Saturday or Sunday).

public static bool IsWeekend(this DateTime date)

Parameters

date DateTime

The DateTime to check.

Returns

bool

True if it is a weekend; otherwise false.

IsWeekend(DateTimeOffset)

Checks if the given DateTimeOffset falls on a weekend (Saturday or Sunday).

public static bool IsWeekend(this DateTimeOffset date)

Parameters

date DateTimeOffset

The DateTimeOffset to check.

Returns

bool

True if it is a weekend; otherwise false.

IsWeekend(DayOfWeek)

Checks if the given DayOfWeek represents a weekend (Saturday or Sunday).

public static bool IsWeekend(this DayOfWeek dow)

Parameters

dow DayOfWeek

The DayOfWeek.

Returns

bool

True if it is a weekend; otherwise false.

MicrosecondsToTicks(long)

Converts microseconds to ticks.

public static long MicrosecondsToTicks(this long mcs)

Parameters

mcs long

Returns

long

NanosecondsToTicks(long)

Converts nanoseconds to ticks.

public static long NanosecondsToTicks(this long nanoseconds)

Parameters

nanoseconds long

Returns

long

Range(DateTime, DateTime, TimeSpan)

Generates a sequence of DateTime values from a start to an end with a given interval.

public static IEnumerable<DateTime> Range(this DateTime from, DateTime to, TimeSpan interval)

Parameters

from DateTime

Start date.

to DateTime

End date.

interval TimeSpan

The interval between generated dates.

Returns

IEnumerable<DateTime>

An enumerable of dates.

StartOfWeek(DateTime, DayOfWeek)

Gets the start of the week for the specified DateTime, based on a chosen DayOfWeek.

public static DateTime StartOfWeek(this DateTime date, DayOfWeek startOfWeek)

Parameters

date DateTime
startOfWeek DayOfWeek

Returns

DateTime

SyncMarketTime(int)

Synchronizes the current offset by comparing local time with an NTP server.

public static void SyncMarketTime(int timeout = 5000)

Parameters

timeout int

The synchronization timeout in milliseconds.

TicksToMicroseconds(long)

Converts ticks to microseconds.

public static long TicksToMicroseconds(this long ticks)

Parameters

ticks long

Returns

long

TicksToNanoseconds(long)

Converts ticks to nanoseconds.

public static long TicksToNanoseconds(this long ticks)

Parameters

ticks long

Returns

long

To(DateTime, TimeZoneInfo, TimeZoneInfo)

Converts a DateTime between time zones.

public static DateTime To(this DateTime time, TimeZoneInfo source = null, TimeZoneInfo destination = null)

Parameters

time DateTime
source TimeZoneInfo
destination TimeZoneInfo

Returns

DateTime

ToDateTime(string, string, CultureInfo)

Converts a string to a DateTime using the provided format.

public static DateTime ToDateTime(this string value, string format, CultureInfo ci = null)

Parameters

value string
format string
ci CultureInfo

Returns

DateTime

ToDateTimeOffset(DateTime, TimeSpan)

Converts a DateTime and a specific offset to a DateTimeOffset.

public static DateTimeOffset ToDateTimeOffset(this DateTime date, TimeSpan offset)

Parameters

date DateTime
offset TimeSpan

Returns

DateTimeOffset

ToDateTimeOffset(DateTime, TimeZoneInfo)

Converts a DateTime and a time zone to a DateTimeOffset.

public static DateTimeOffset ToDateTimeOffset(this DateTime date, TimeZoneInfo zone)

Parameters

date DateTime
zone TimeZoneInfo

Returns

DateTimeOffset

ToDateTimeOffset(string, string, CultureInfo)

Converts a string to a DateTimeOffset using the provided format.

public static DateTimeOffset ToDateTimeOffset(this string value, string format, CultureInfo ci = null)

Parameters

value string
format string
ci CultureInfo

Returns

DateTimeOffset

ToIso8601(DateTime, IFormatProvider)

Formats the specified DateTime as an ISO8601 string.

public static string ToIso8601(this DateTime dt, IFormatProvider provider = null)

Parameters

dt DateTime

The DateTime to format.

provider IFormatProvider

An optional IFormatProvider.

Returns

string

An ISO8601-formatted string.

ToJulianDate(DateTime)

Converts the specified DateTime to a Julian date.

public static double ToJulianDate(this DateTime date)

Parameters

date DateTime

The DateTime to convert.

Returns

double

A double representing the Julian date.

ToLocalTime(DateTimeOffset, TimeZoneInfo)

Converts a DateTimeOffset to a DateTime in the specified time zone.

public static DateTime ToLocalTime(this DateTimeOffset dto, TimeZoneInfo zone)

Parameters

dto DateTimeOffset
zone TimeZoneInfo

Returns

DateTime

ToNanoseconds(DateTime)

Converts a DateTime to the total number of nanoseconds.

public static long ToNanoseconds(this DateTime dt)

Parameters

dt DateTime

Returns

long

ToNanoseconds(TimeSpan)

Converts a TimeSpan to the total number of nanoseconds.

public static long ToNanoseconds(this TimeSpan ts)

Parameters

ts TimeSpan

Returns

long

ToTimeSpan(string, string, CultureInfo)

Converts a string to a TimeSpan using the provided format.

public static TimeSpan ToTimeSpan(this string value, string format, CultureInfo ci = null)

Parameters

value string
format string
ci CultureInfo

Returns

TimeSpan

ToUnix(DateTime, bool)

Converts the specified DateTime to Unix time in seconds or milliseconds.

public static double ToUnix(this DateTime time, bool isSeconds = true)

Parameters

time DateTime

The DateTime to convert.

isSeconds bool

If set to true, returns seconds; otherwise milliseconds.

Returns

double

A double representing the Unix time.

ToUnix(DateTimeOffset, bool)

Converts the specified DateTimeOffset to Unix time in seconds or milliseconds.

public static double ToUnix(this DateTimeOffset time, bool isSeconds = true)

Parameters

time DateTimeOffset

The DateTimeOffset to convert.

isSeconds bool

If set to true, returns seconds; otherwise milliseconds.

Returns

double

A double representing the Unix time.

ToUnixMcs(DateTime)

Converts the specified DateTime to Unix Microseconds since GregorianStart.

public static long ToUnixMcs(this DateTime time)

Parameters

time DateTime

The DateTime to convert.

Returns

long

A long representing the Unix time in microseconds.

TotalCenturies(TimeSpan)

Returns total centuries in the specified TimeSpan.

public static double TotalCenturies(this TimeSpan value)

Parameters

value TimeSpan

Returns

double

TotalMilleniums(TimeSpan)

Returns total millenniums in the specified TimeSpan.

public static double TotalMilleniums(this TimeSpan value)

Parameters

value TimeSpan

Returns

double

TotalMonths(TimeSpan)

Returns total months in the specified TimeSpan.

public static double TotalMonths(this TimeSpan value)

Parameters

value TimeSpan

Returns

double

TotalWeeks(TimeSpan)

Returns total weeks in the specified TimeSpan.

public static double TotalWeeks(this TimeSpan value)

Parameters

value TimeSpan

Returns

double

TotalYears(TimeSpan)

Returns total years in the specified TimeSpan.

public static double TotalYears(this TimeSpan value)

Parameters

value TimeSpan

Returns

double

Truncate(DateTime, long)

Truncates a DateTime to the specified ticks precision.

public static DateTime Truncate(this DateTime time, long precision)

Parameters

time DateTime
precision long

Returns

DateTime

Truncate(DateTime, TimeSpan)

Truncates a DateTime to the specified time span precision.

public static DateTime Truncate(this DateTime dateTime, TimeSpan timeSpan)

Parameters

dateTime DateTime
timeSpan TimeSpan

Returns

DateTime

Truncate(DateTimeOffset, long)

Truncates the specified DateTimeOffset to the given precision in ticks.

public static DateTimeOffset Truncate(this DateTimeOffset time, long precision)

Parameters

time DateTimeOffset

The DateTimeOffset to truncate.

precision long

The precision in ticks.

Returns

DateTimeOffset

The truncated DateTimeOffset.

Truncate(DateTimeOffset, TimeSpan)

Truncates the specified DateTimeOffset to the given TimeSpan.

public static DateTimeOffset Truncate(this DateTimeOffset time, TimeSpan timeSpan)

Parameters

time DateTimeOffset

The DateTimeOffset to truncate.

timeSpan TimeSpan

The TimeSpan precision.

Returns

DateTimeOffset

The truncated DateTimeOffset.

Truncate(TimeSpan, long)

Truncates a TimeSpan to the specified ticks precision.

public static TimeSpan Truncate(this TimeSpan time, long precision)

Parameters

time TimeSpan
precision long

Returns

TimeSpan

Truncate(TimeSpan, TimeSpan)

Truncates a TimeSpan to the specified time span precision.

public static TimeSpan Truncate(this TimeSpan dateTime, TimeSpan timeSpan)

Parameters

dateTime TimeSpan
timeSpan TimeSpan

Returns

TimeSpan

TryFromUnix(double, bool)

Tries to create a DateTime from a Unix time in seconds or milliseconds. Returns null if time is near zero.

public static DateTime? TryFromUnix(this double time, bool isSeconds = true)

Parameters

time double

The Unix time to convert.

isSeconds bool

If set to true, interprets time as seconds; otherwise milliseconds.

Returns

DateTime?

A DateTime in UTC, or null if near zero.

TryFromUnix(long, bool)

Tries to create a DateTime from a Unix time in seconds or milliseconds. Returns null if time is 0.

public static DateTime? TryFromUnix(this long time, bool isSeconds = true)

Parameters

time long

The Unix time to convert.

isSeconds bool

If set to true, interprets time as seconds; otherwise milliseconds.

Returns

DateTime?

A DateTime in UTC, or null if 0.

TryToDateTime(string, string, CultureInfo)

Attempts to convert a string to a DateTime using the provided format.

public static DateTime? TryToDateTime(this string value, string format, CultureInfo ci = null)

Parameters

value string
format string
ci CultureInfo

Returns

DateTime?

TryToDateTimeOffset(string, string, CultureInfo)

Attempts to convert a string to a DateTimeOffset using the provided format.

public static DateTimeOffset? TryToDateTimeOffset(this string value, string format, CultureInfo ci = null)

Parameters

value string
format string
ci CultureInfo

Returns

DateTimeOffset?

TryToTimeSpan(string, string, CultureInfo)

Attempts to convert a string to a TimeSpan using the provided format.

public static TimeSpan? TryToTimeSpan(this string value, string format, CultureInfo ci = null)

Parameters

value string
format string
ci CultureInfo

Returns

TimeSpan?

UtcKind(DateTime)

Converts a DateTime to UTC kind.

public static DateTime UtcKind(this DateTime date)

Parameters

date DateTime

Returns

DateTime