Class TimeHelper
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
Cst
Represents the Central Standard Time zone.
public static readonly TimeZoneInfo Cst
Field Value
Day
A TimeSpan of one day.
public static readonly TimeSpan Day
Field Value
Est
Represents the Eastern Standard Time zone.
public static readonly TimeZoneInfo Est
Field Value
Fle
Represents the FLE (Helsinki, Kyiv, Riga, Sofia, Tallinn, Vilnius) Standard Time zone.
public static readonly TimeZoneInfo Fle
Field Value
Gmt
Represents the GMT Standard Time zone.
public static readonly TimeZoneInfo Gmt
Field Value
GregorianStart
Represents a reference start date (1/1/1970, UTC).
public static readonly DateTime GregorianStart
Field Value
Hour
A TimeSpan of one hour.
public static readonly TimeSpan Hour
Field Value
Korea
Represents the Korea Standard Time zone.
public static readonly TimeZoneInfo Korea
Field Value
LessOneDay
A TimeSpan that is one tick less than a day.
public static readonly TimeSpan LessOneDay
Field Value
Minute1
A TimeSpan of one minute.
public static readonly TimeSpan Minute1
Field Value
Minute10
A TimeSpan of ten minutes.
public static readonly TimeSpan Minute10
Field Value
Minute15
A TimeSpan of fifteen minutes.
public static readonly TimeSpan Minute15
Field Value
Minute5
A TimeSpan of five minutes.
public static readonly TimeSpan Minute5
Field Value
Month
A TimeSpan of one month (30 days).
public static readonly TimeSpan Month
Field Value
Moscow
Represents the Russian Standard Time zone (Moscow).
public static readonly TimeZoneInfo Moscow
Field Value
NanosecondsPerTick
Represents the number of nanoseconds in 1 tick.
public const long NanosecondsPerTick = 100
Field Value
TicksPerCentury
Represents the number of ticks in 1 century.
public const long TicksPerCentury = 31536000000000000
Field Value
TicksPerMicrosecond
Represents the number of ticks in 1 microsecond.
public const long TicksPerMicrosecond = 10
Field Value
TicksPerMillenium
Represents the number of ticks in 1 millenium.
public const long TicksPerMillenium = 315360000000000000
Field Value
TicksPerMonth
Represents the number of ticks in 1 month.
public const long TicksPerMonth = 25920000000000
Field Value
TicksPerNanosecond
Represents the number of ticks in 1 nanosecond.
public const double TicksPerNanosecond = 0.01
Field Value
TicksPerWeek
Represents the number of ticks in 1 week.
public const long TicksPerWeek = 6048000000000
Field Value
TicksPerYear
Represents the number of ticks in 1 year.
public const long TicksPerYear = 315360000000000
Field Value
Tokyo
Represents the Tokyo Standard Time zone.
public static readonly TimeZoneInfo Tokyo
Field Value
Tunisia
Represents the West Central Africa Standard Time zone (Tunisia).
public static readonly TimeZoneInfo Tunisia
Field Value
Week
A TimeSpan of one week (7 days).
public static readonly TimeSpan Week
Field Value
Year
A TimeSpan of one year (365 days).
public static readonly TimeSpan Year
Field Value
Properties
Now
Gets the current time including the configured offset.
public static DateTime Now { get; }
Property Value
NowOffset
Gets or sets the offset applied to the current time when retrieving Now.
public static TimeSpan NowOffset { get; set; }
Property Value
NowWithOffset
Gets the current time as a DateTimeOffset including the local offset.
public static DateTimeOffset NowWithOffset { get; }
Property Value
TimeZoneOffset
Gets or sets the time zone offset used for calculations.
public static TimeSpan TimeZoneOffset { get; set; }
Property Value
UnixNowMls
Gets the current Unix time in milliseconds.
public static double UnixNowMls { get; }
Property Value
UnixNowS
Gets the current Unix time in seconds.
public static double UnixNowS { get; }
Property Value
Methods
AddMicroseconds(DateTime, long)
Adds the specified number of microseconds to a DateTime.
public static DateTime AddMicroseconds(this DateTime dt, long microseconds)
Parameters
Returns
AddMicroseconds(DateTimeOffset, long)
Adds the specified number of microseconds to a DateTimeOffset.
public static DateTimeOffset AddMicroseconds(this DateTimeOffset dto, long microseconds)
Parameters
dto
DateTimeOffsetmicroseconds
long
Returns
AddMicroseconds(TimeSpan, long)
Adds the specified number of microseconds to a TimeSpan.
public static TimeSpan AddMicroseconds(this TimeSpan t, long microseconds)
Parameters
Returns
AddNanoseconds(DateTime, long)
Adds the specified number of nanoseconds to a DateTime.
public static DateTime AddNanoseconds(this DateTime dt, long nanoseconds)
Parameters
Returns
AddNanoseconds(DateTimeOffset, long)
Adds the specified number of nanoseconds to a DateTimeOffset.
public static DateTimeOffset AddNanoseconds(this DateTimeOffset dto, long nanoseconds)
Parameters
dto
DateTimeOffsetnanoseconds
long
Returns
AddNanoseconds(TimeSpan, long)
Adds the specified number of nanoseconds to a TimeSpan.
public static TimeSpan AddNanoseconds(this TimeSpan t, long nanoseconds)
Parameters
Returns
ApplyChina(DateTime)
Creates a DateTimeOffset from a DateTime in China Standard Time.
public static DateTimeOffset ApplyChina(this DateTime dt)
Parameters
dt
DateTime
Returns
ApplyEst(DateTime)
Creates a DateTimeOffset from a DateTime in Eastern Standard Time.
public static DateTimeOffset ApplyEst(this DateTime dt)
Parameters
dt
DateTime
Returns
ApplyLocal(DateTime)
Creates a DateTimeOffset from a DateTime using the local time zone.
public static DateTimeOffset ApplyLocal(this DateTime dt)
Parameters
dt
DateTime
Returns
ApplyMoscow(DateTime)
Creates a DateTimeOffset from a DateTime in Russian Standard Time (Moscow).
public static DateTimeOffset ApplyMoscow(this DateTime dt)
Parameters
dt
DateTime
Returns
ApplyTimeZone(DateTime, TimeSpan)
Creates a DateTimeOffset from a DateTime with the specified offset.
public static DateTimeOffset ApplyTimeZone(this DateTime dt, TimeSpan offset)
Parameters
Returns
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
DateTimezone
TimeZoneInfo
Returns
ApplyUtc(DateTime)
Creates a DateTimeOffset from a DateTime using UTC.
public static DateTimeOffset ApplyUtc(this DateTime dt)
Parameters
dt
DateTime
Returns
ChangeKind(DateTime, DateTimeKind)
Changes the DateTimeKind of a DateTime.
public static DateTime ChangeKind(this DateTime date, DateTimeKind kind = DateTimeKind.Unspecified)
Parameters
date
DateTimekind
DateTimeKind
Returns
Convert(DateTimeOffset, TimeZoneInfo)
Converts the specified DateTimeOffset to the provided time zone.
public static DateTimeOffset Convert(this DateTimeOffset dto, TimeZoneInfo zone)
Parameters
dto
DateTimeOffsetThe DateTimeOffset to convert.
zone
TimeZoneInfoThe 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
ConvertToEst(DateTimeOffset)
Converts the specified DateTimeOffset to Eastern Standard Time.
public static DateTimeOffset ConvertToEst(this DateTimeOffset dto)
Parameters
dto
DateTimeOffsetThe 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
DateTimeOffsetThe 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
DateTimeOffsetThe 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
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
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
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
DateTimeformat
stringci
CultureInfo
Returns
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
DateTimeOffsetformat
stringci
CultureInfo
Returns
FromIso8601(string, IFormatProvider)
Parses the specified string as an ISO8601 date/time.
public static DateTime FromIso8601(this string str, IFormatProvider provider = null)
Parameters
str
stringThe string to parse.
provider
IFormatProviderAn optional IFormatProvider.
Returns
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
TimeSpanformat
stringci
CultureInfo
Returns
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
doubleThe Unix time to convert.
isSeconds
boolIf set to true, interprets time as seconds; otherwise milliseconds.
Returns
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
longThe Unix time to convert.
isSeconds
boolIf set to true, interprets time as seconds; otherwise milliseconds.
Returns
FromUnixMcs(double)
Creates a DateTime from Microseconds since GregorianStart.
public static DateTime FromUnixMcs(this double mcs)
Parameters
mcs
doubleMicroseconds to convert.
Returns
FromUnixMcs(long)
Creates a DateTime from Microseconds since GregorianStart.
public static DateTime FromUnixMcs(this long mcs)
Parameters
mcs
longMicroseconds to convert.
Returns
GetIso8601WeekOfYear(DateTime, CultureInfo)
Calculates the ISO8601 week number of the specified date.
public static int GetIso8601WeekOfYear(this DateTime time, CultureInfo ci = null)
Parameters
time
DateTimeThe DateTime to evaluate.
ci
CultureInfoAn 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
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
GetMicroseconds(TimeSpan)
Gets the microseconds component from a TimeSpan.
public static int GetMicroseconds(this TimeSpan ts)
Parameters
ts
TimeSpan
Returns
GetNanoseconds(DateTime)
Gets the nanoseconds component from a DateTime.
public static int GetNanoseconds(this DateTime dt)
Parameters
dt
DateTime
Returns
GetNanoseconds(long)
Gets the nanoseconds component from the specified number of ticks.
public static int GetNanoseconds(this long ticks)
Parameters
ticks
long
Returns
GetNanoseconds(TimeSpan)
Gets the nanoseconds component from a TimeSpan.
public static int GetNanoseconds(this TimeSpan ts)
Parameters
ts
TimeSpan
Returns
GetUnixDiff(DateTime)
Gets the difference between the specified DateTime and the GregorianStart.
public static TimeSpan GetUnixDiff(this DateTime time)
Parameters
Returns
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
Returns
IsDateTime(Type)
Checks if the given Type is a date/time type.
public static bool IsDateTime(this Type type)
Parameters
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
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
DateTimeOffsetThe 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
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
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
DateTimeOffsetThe 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
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
NanosecondsToTicks(long)
Converts nanoseconds to ticks.
public static long NanosecondsToTicks(this long nanoseconds)
Parameters
nanoseconds
long
Returns
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
DateTimeStart date.
to
DateTimeEnd date.
interval
TimeSpanThe interval between generated dates.
Returns
- IEnumerable<DateTime>
An enumerable of dates.
StartOfWeek(DateTime, DayOfWeek)
public static DateTime StartOfWeek(this DateTime date, DayOfWeek startOfWeek)
Parameters
Returns
SyncMarketTime(int)
Synchronizes the current offset by comparing local time with an NTP server.
public static void SyncMarketTime(int timeout = 5000)
Parameters
timeout
intThe synchronization timeout in milliseconds.
TicksToMicroseconds(long)
Converts ticks to microseconds.
public static long TicksToMicroseconds(this long ticks)
Parameters
ticks
long
Returns
TicksToNanoseconds(long)
Converts ticks to nanoseconds.
public static long TicksToNanoseconds(this long ticks)
Parameters
ticks
long
Returns
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
DateTimesource
TimeZoneInfodestination
TimeZoneInfo
Returns
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
stringformat
stringci
CultureInfo
Returns
ToDateTimeOffset(DateTime, TimeSpan)
Converts a DateTime and a specific offset to a DateTimeOffset.
public static DateTimeOffset ToDateTimeOffset(this DateTime date, TimeSpan offset)
Parameters
Returns
ToDateTimeOffset(DateTime, TimeZoneInfo)
Converts a DateTime and a time zone to a DateTimeOffset.
public static DateTimeOffset ToDateTimeOffset(this DateTime date, TimeZoneInfo zone)
Parameters
date
DateTimezone
TimeZoneInfo
Returns
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
stringformat
stringci
CultureInfo
Returns
ToIso8601(DateTime, IFormatProvider)
Formats the specified DateTime as an ISO8601 string.
public static string ToIso8601(this DateTime dt, IFormatProvider provider = null)
Parameters
dt
DateTimeThe DateTime to format.
provider
IFormatProviderAn 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
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
DateTimeOffsetzone
TimeZoneInfo
Returns
ToNanoseconds(DateTime)
Converts a DateTime to the total number of nanoseconds.
public static long ToNanoseconds(this DateTime dt)
Parameters
dt
DateTime
Returns
ToNanoseconds(TimeSpan)
Converts a TimeSpan to the total number of nanoseconds.
public static long ToNanoseconds(this TimeSpan ts)
Parameters
ts
TimeSpan
Returns
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
stringformat
stringci
CultureInfo
Returns
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
DateTimeThe DateTime to convert.
isSeconds
boolIf 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
DateTimeOffsetThe DateTimeOffset to convert.
isSeconds
boolIf 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
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
TotalMilleniums(TimeSpan)
Returns total millenniums in the specified TimeSpan.
public static double TotalMilleniums(this TimeSpan value)
Parameters
value
TimeSpan
Returns
TotalMonths(TimeSpan)
Returns total months in the specified TimeSpan.
public static double TotalMonths(this TimeSpan value)
Parameters
value
TimeSpan
Returns
TotalWeeks(TimeSpan)
Returns total weeks in the specified TimeSpan.
public static double TotalWeeks(this TimeSpan value)
Parameters
value
TimeSpan
Returns
TotalYears(TimeSpan)
Returns total years in the specified TimeSpan.
public static double TotalYears(this TimeSpan value)
Parameters
value
TimeSpan
Returns
Truncate(DateTime, long)
Truncates a DateTime to the specified ticks precision.
public static DateTime Truncate(this DateTime time, long precision)
Parameters
Returns
Truncate(DateTime, TimeSpan)
Truncates a DateTime to the specified time span precision.
public static DateTime Truncate(this DateTime dateTime, TimeSpan timeSpan)
Parameters
Returns
Truncate(DateTimeOffset, long)
Truncates the specified DateTimeOffset to the given precision in ticks.
public static DateTimeOffset Truncate(this DateTimeOffset time, long precision)
Parameters
time
DateTimeOffsetThe DateTimeOffset to truncate.
precision
longThe 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
DateTimeOffsetThe DateTimeOffset to truncate.
timeSpan
TimeSpanThe 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
Returns
Truncate(TimeSpan, TimeSpan)
Truncates a TimeSpan to the specified time span precision.
public static TimeSpan Truncate(this TimeSpan dateTime, TimeSpan timeSpan)
Parameters
Returns
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
doubleThe Unix time to convert.
isSeconds
boolIf set to true, interprets time as seconds; otherwise milliseconds.
Returns
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
longThe Unix time to convert.
isSeconds
boolIf set to true, interprets time as seconds; otherwise milliseconds.
Returns
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
stringformat
stringci
CultureInfo
Returns
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
stringformat
stringci
CultureInfo
Returns
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
stringformat
stringci
CultureInfo
Returns
UtcKind(DateTime)
Converts a DateTime to UTC kind.
public static DateTime UtcKind(this DateTime date)
Parameters
date
DateTime