Class DateTimeExtensions
Extension methods for DateTime.
Inherited Members
Namespace: GlitchedPolygons.ExtensionMethods
Assembly: GlitchedPolygons.ExtensionMethods.dll
Syntax
public static class DateTimeExtensions
Methods
AlmostEquals(DateTime, DateTime, double)
Declaration
public static bool AlmostEquals(this DateTime dt1, DateTime dt2, double threshold = 1)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | dt1 | The DateTime to compare. |
| DateTime | dt2 | The DateTime to compare. |
| double | threshold | The equality-defining threshold in seconds between the two DateTimes. If the two DateTimes are further apart from each other than this amount of seconds, they're not equal. |
Returns
| Type | Description |
|---|---|
| bool | Whether the two DateTime objects are almost equal. |
FromUnixTimeMilliseconds(long)
Converts a Unix timestamp (milliseconds since 1970-01-01 00:00:00.000 UTC) to a DateTime (in UTC).
Declaration
public static DateTime FromUnixTimeMilliseconds(long timestamp)
Parameters
| Type | Name | Description |
|---|---|---|
| long | timestamp | The Unix timestamp to convert. |
Returns
| Type | Description |
|---|---|
| DateTime | The converted DateTime in UTC. |
FromUnixTimeSeconds(long)
Converts a Unix timestamp (seconds since 1970-01-01T00:00:00Z) to a UTC DateTime.
Declaration
public static DateTime FromUnixTimeSeconds(long timestamp)
Parameters
| Type | Name | Description |
|---|---|---|
| long | timestamp | The Unix timestamp to convert. |
Returns
| Type | Description |
|---|---|
| DateTime | The converted DateTime in UTC. |
ToRfc3339String(DateTime)
Converts DateTime to a RFC3339 string.
Declaration
public static string ToRfc3339String(this DateTime dateTime)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | dateTime | The DateTime to stringify. |
Returns
| Type | Description |
|---|---|
| string | The DateTime as a RFC3339 |
ToSwissTime(DateTime)
Converts a DateTime to Swiss local time (UTC+1 in winter and UTC+2 in summer).
Declaration
public static DateTime ToSwissTime(this DateTime dt)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | dt | DateTime to convert. |
Returns
| Type | Description |
|---|---|
| DateTime | The passed DateTime with a UTC offset applied that makes the resulting DateTime local to Switzerland. |
ToUnixTimeMilliseconds(DateTime)
Converts a <xref href="System.DateTime" data-throw-if-not-resolved="false"></xref> to Unix time (milliseconds since 1970-01-01T00:00:00Z).
Make sure that the <xref href="System.DateTime" data-throw-if-not-resolved="false"></xref> you're converting is UTC!
Declaration
public static long ToUnixTimeMilliseconds(this DateTime dt)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | dt | The DateTime to convert. |
Returns
| Type | Description |
|---|---|
| long | Unix time (milliseconds since 1970-01-01T00:00:00Z) |
ToUnixTimeSeconds(DateTime)
Converts a <xref href="System.DateTime" data-throw-if-not-resolved="false"></xref> to Unix time (seconds since 1970-01-01T00:00:00Z).
Make sure that the <xref href="System.DateTime" data-throw-if-not-resolved="false"></xref> you're converting is in UTC!
Declaration
public static long ToUnixTimeSeconds(this DateTime dt)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | dt | The DateTime to convert. |
Returns
| Type | Description |
|---|---|
| long | Unix time (seconds since 1970-01-01T00:00:00Z) |
ToUnixTimeSeconds(DateTime, string)
Converts a DateTime, interpreted as local time in the specified time zone, to Unix time (the number of whole seconds since 1970-01-01T00:00:00Z).
Declaration
public static long ToUnixTimeSeconds(this DateTime dateTime, string timeZoneId)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | dateTime | The date and time to convert. Its Kind is ignored/overridden —
the value is treated as unspecified/local time in |
| string | timeZoneId | The IANA time zone identifier (e.g. |
Returns
| Type | Description |
|---|---|
| long | The number of seconds elapsed since the Unix epoch (1970-01-01T00:00:00Z),
corresponding to |
Exceptions
| Type | Condition |
|---|---|
| TimeZoneNotFoundException | Thrown if |
| InvalidTimeZoneException | Thrown if the time zone data on the system is corrupt. |