Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Class DateTimeConverter

Helper class to handler date time from and to local and utc.

Inheritance
System.Object
DateTimeConverter
Inherited Members
System.Object.ToString()
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
Namespace: EPiServer.Data
Assembly: EPiServer.Data.dll
Version: 11.20.7
Syntax
public abstract class DateTimeConverter

Constructors

DateTimeConverter()

Declaration
protected DateTimeConverter()

Properties

MaxValue

Gets the highest value that can be saved.

Declaration
public static DateTime MaxValue { get; }
Property Value
Type Description
System.DateTime

The highest value that can be saved.

Remarks

The highest value is currently 9999-12-31.

MinValue

Gets the lowest value that can be saved.

Declaration
public static DateTime MinValue { get; }
Property Value
Type Description
System.DateTime

The lowest value that can be saved.

Remarks

The lowest value is currently 1753-01-01.

Methods

ParseToUtc(String)

Converts the specified string representation of a date and time to its System.DateTime equivalent, with a System.DateTime.Kind value of DateTimeKind.Utc.

Declaration
public abstract DateTime ParseToUtc(string value)
Parameters
Type Name Description
System.String value

A string containing a date and time to convert.

Returns
Type Description
System.DateTime

An object that is equivalent to the date and time contained in value.

Remarks

If a call to DateTime.Parse(string) with the same parameter would return a value with System.DateTime.Kind equal to DateTimeKind.Local, then the value is converted to local time; otherise, the result value is specified as UTC.

Exceptions
Type Condition
System.ArgumentNullException

value is null.

System.FormatException

value does not contain a valid string representation of a date and time.

ParseToUtc(String, IFormatProvider)

Converts the specified string representation of a date and time to its System.DateTime equivalent using the specified culture-specific format information, with a System.DateTime.Kind value of DateTimeKind.Utc.

Declaration
public abstract DateTime ParseToUtc(string value, IFormatProvider provider)
Parameters
Type Name Description
System.String value

A string containing a date and time to convert.

System.IFormatProvider provider

An object that supplies culture-specific format information about value.

Returns
Type Description
System.DateTime

An object that is equivalent to the date and time contained in s as specified by provider.

Remarks

If a call to DateTime.Parse(string) with the same parameter would return a value with System.DateTime.Kind equal to DateTimeKind.Local, then the value is converted to local time; otherise, the result value is specified as UTC.

Exceptions
Type Condition
System.ArgumentNullException

value is null.

System.FormatException

value does not contain a valid string representation of a date and time.

ReadDatabaseValueAsLocal(Object)

Read an object returned from a database call to a Local DateTime value.

Declaration
public abstract DateTime? ReadDatabaseValueAsLocal(object value)
Parameters
Type Name Description
System.Object value

An object representing a DateTime returned from a database call.

Returns
Type Description
System.Nullable<System.DateTime>

The converted value, or null if value is null or equal to DBNull.Value.

ReadDatabaseValueAsUtc(Object)

Read an object returned from a database call to a UTC DateTime value.

Declaration
public abstract DateTime? ReadDatabaseValueAsUtc(object value)
Parameters
Type Name Description
System.Object value

An object representing a DateTime returned from a database call.

Returns
Type Description
System.Nullable<System.DateTime>

The converted value, or null if value is null or equal to DBNull.Value.

ToLocalTime(DateTime, Boolean)

Converts value to local time.

Declaration
public abstract DateTime ToLocalTime(DateTime value, bool convertUnspecified)
Parameters
Type Name Description
System.DateTime value

The value to convert.

System.Boolean convertUnspecified

If true, values with DateTime.Kind set to DateTimeKind.Unspecified will be converted to local time (the same behavior as DateTime.ToLocalTime); if false, they will only have DateTime.Kind set to DateTimeKind.Local.

Returns
Type Description
System.DateTime

The possibly converted DateTime value.

ToUniversalTime(DateTime, Boolean)

Converts value to universal time.

Declaration
public abstract DateTime ToUniversalTime(DateTime value, bool convertUnspecified)
Parameters
Type Name Description
System.DateTime value

The value to convert.

System.Boolean convertUnspecified

If true, values with DateTime.Kind set to DateTimeKind.Unspecified will be converted to universal time (the same behavior as DateTime.ToUniversalTime); if false, they will only have DateTime.Kind set to DateTimeKind.Utc.

Returns
Type Description
System.DateTime

The possibly converted DateTime value.

TryParseToUtc(String, out DateTime)

Converts the specified string representation of a date and time to its System.DateTime equivalent, with a System.DateTime.Kind value of DateTimeKind.Utc; and returns a value that indicates whether the conversion succeeded.

Declaration
public abstract bool TryParseToUtc(string value, out DateTime result)
Parameters
Type Name Description
System.String value

A string containing a date and time to convert.

System.DateTime result

When this method returns, contains the System.DateTime value equivalent to the date and time contained in value, if the conversion succeeded, or DateTime.MinValue if the conversion failed. The conversion fails if the value parameter is null, is an empty string (""), or does not contain a valid string representation of a date and time. This parameter is passed uninitialized.

Returns
Type Description
System.Boolean

True if the value parameter was converted successfully; otherwise, false.

Remarks

If a call to DateTime.Parse(string) with the same parameter would return a value with System.DateTime.Kind equal to DateTimeKind.Local, then the value is converted to local time; otherise, the result value is specified as UTC.

Exceptions
Type Condition
System.ArgumentOutOfRangeException

The date is in Japanese Emperor Year (Wareki) format and the year is out of range.

Extension Methods