DateTime 結構

定義

表示時間的瞬間,通常以一天的日期和時間表示。

public value class DateTime : IComparable, IComparable<DateTime>, IConvertible, IEquatable<DateTime>, IFormattable
public value class DateTime : IComparable, IComparable<DateTime>, IConvertible, IEquatable<DateTime>, IFormattable, System::Runtime::Serialization::ISerializable
public value class DateTime : IComparable, IComparable<DateTime>, IConvertible, IEquatable<DateTime>, ISpanFormattable, System::Runtime::Serialization::ISerializable
public value class DateTime : IComparable, IComparable<DateTime>, IConvertible, IEquatable<DateTime>, IParsable<DateTime>, ISpanFormattable, ISpanParsable<DateTime>, System::Runtime::Serialization::ISerializable
public value class DateTime : IComparable, IComparable<DateTime>, IConvertible, IEquatable<DateTime>, IParsable<DateTime>, ISpanFormattable, ISpanParsable<DateTime>, IUtf8SpanFormattable, System::Runtime::Serialization::ISerializable
public value class DateTime : IComparable, IConvertible, IFormattable
public value class DateTime : IComparable, IComparable<DateTime>, IEquatable<DateTime>, IFormattable
public struct DateTime : IComparable, IComparable<DateTime>, IConvertible, IEquatable<DateTime>, IFormattable
public readonly struct DateTime : IComparable, IComparable<DateTime>, IConvertible, IEquatable<DateTime>, IFormattable, System.Runtime.Serialization.ISerializable
public readonly struct DateTime : IComparable, IComparable<DateTime>, IConvertible, IEquatable<DateTime>, ISpanFormattable, System.Runtime.Serialization.ISerializable
public readonly struct DateTime : IComparable, IComparable<DateTime>, IConvertible, IEquatable<DateTime>, IParsable<DateTime>, ISpanFormattable, ISpanParsable<DateTime>, System.Runtime.Serialization.ISerializable
public readonly struct DateTime : IComparable, IComparable<DateTime>, IConvertible, IEquatable<DateTime>, IParsable<DateTime>, ISpanFormattable, ISpanParsable<DateTime>, IUtf8SpanFormattable, System.Runtime.Serialization.ISerializable
public struct DateTime : IComparable, IComparable<DateTime>, IConvertible, IEquatable<DateTime>, IFormattable, System.Runtime.Serialization.ISerializable
[System.Serializable]
public struct DateTime : IComparable, IConvertible, IFormattable
[System.Serializable]
public struct DateTime : IComparable, IComparable<DateTime>, IConvertible, IEquatable<DateTime>, IFormattable, System.Runtime.Serialization.ISerializable
public struct DateTime : IComparable, IComparable<DateTime>, IEquatable<DateTime>, IFormattable
type DateTime = struct
    interface IConvertible
    interface IFormattable
type DateTime = struct
    interface IConvertible
    interface IFormattable
    interface ISerializable
type DateTime = struct
    interface IConvertible
    interface ISpanFormattable
    interface IFormattable
    interface ISerializable
type DateTime = struct
    interface IConvertible
    interface IFormattable
    interface IParsable<DateTime>
    interface ISpanFormattable
    interface ISpanParsable<DateTime>
    interface ISerializable
type DateTime = struct
    interface IConvertible
    interface IFormattable
    interface IParsable<DateTime>
    interface ISpanFormattable
    interface ISpanParsable<DateTime>
    interface ISerializable
    interface IUtf8SpanFormattable
type DateTime = struct
    interface IConvertible
    interface IFormattable
    interface IParsable<DateTime>
    interface ISpanFormattable
    interface ISpanParsable<DateTime>
    interface IUtf8SpanFormattable
    interface ISerializable
[<System.Serializable>]
type DateTime = struct
    interface IFormattable
    interface IConvertible
[<System.Serializable>]
type DateTime = struct
    interface IFormattable
    interface IConvertible
    interface ISerializable
type DateTime = struct
    interface IFormattable
Public Structure DateTime
Implements IComparable, IComparable(Of DateTime), IConvertible, IEquatable(Of DateTime), IFormattable
Public Structure DateTime
Implements IComparable, IComparable(Of DateTime), IConvertible, IEquatable(Of DateTime), IFormattable, ISerializable
Public Structure DateTime
Implements IComparable, IComparable(Of DateTime), IConvertible, IEquatable(Of DateTime), ISerializable, ISpanFormattable
Public Structure DateTime
Implements IComparable, IComparable(Of DateTime), IConvertible, IEquatable(Of DateTime), IParsable(Of DateTime), ISerializable, ISpanFormattable, ISpanParsable(Of DateTime)
Public Structure DateTime
Implements IComparable, IComparable(Of DateTime), IConvertible, IEquatable(Of DateTime), IParsable(Of DateTime), ISerializable, ISpanFormattable, ISpanParsable(Of DateTime), IUtf8SpanFormattable
Public Structure DateTime
Implements IComparable, IConvertible, IFormattable
Public Structure DateTime
Implements IComparable, IComparable(Of DateTime), IEquatable(Of DateTime), IFormattable
繼承
DateTime
屬性
實作

備註

如需此 API 的詳細資訊,請參閱 DateTime 的補充 API 備註

建構函式

DateTime(DateOnly, TimeOnly)

將結構的新實體 DateTime 初始化為指定的 DateOnlyTimeOnly。 新的實例將具有 種類 Unspecified

DateTime(DateOnly, TimeOnly, DateTimeKind)

結構的新實體 DateTime 初始化為指定的 DateOnlyTimeOnly 並遵守指定的 DateTimeKind

DateTime(Int32, Int32, Int32)

DateTime 結構的新執行個體初始化為指定的年、月和日。

DateTime(Int32, Int32, Int32, Calendar)

DateTime 結構的新執行個體初始化為指定行事曆的指定年、月和日。

DateTime(Int32, Int32, Int32, Int32, Int32, Int32)

DateTime 結構的新執行個體初始化為指定的年、月、日、時、分和秒。

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Calendar)

DateTime 結構的新執行個體初始化為指定行事曆的指定年、月、日、時、分和秒。

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, DateTimeKind)

DateTime 結構的新執行個體初始化為指定年、月、日、時、分、秒以及國際標準時間 (UTC) 或本地時間。

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32)

DateTime 結構的新執行個體初始化為指定的年、月、日、時、分、秒和毫秒。

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar)

DateTime 結構的新執行個體初始化為指定行事曆的指定年、月、日、時、分、秒和毫秒。

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, DateTimeKind)

DateTime 結構的新執行個體初始化為指定行事曆的指定年、月、日、時、分、秒、毫秒以及國際標準時間 (UTC) 或本地時間。

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, DateTimeKind)

DateTime 結構的新執行個體初始化為指定年、月、日、時、分、秒、毫秒以及國際標準時間 (UTC) 或本地時間。

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32)

DateTime 結構的新執行個體初始化為指定行事曆的指定年、月、日、時、分、秒、毫秒以及國際標準時間 (UTC) 或本地時間。

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar)

DateTime 結構的新執行個體初始化為指定行事曆的指定年、月、日、時、分、秒、毫秒以及國際標準時間 (UTC) 或本地時間。

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Calendar, DateTimeKind)

DateTime 結構的新執行個體初始化為指定行事曆的指定年、月、日、時、分、秒、毫秒以及國際標準時間 (UTC) 或本地時間。

DateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, DateTimeKind)

DateTime 結構的新執行個體初始化為指定行事曆的指定年、月、日、時、分、秒、毫秒以及國際標準時間 (UTC) 或本地時間。

DateTime(Int64)

DateTime 結構的新執行個體初始化為刻度的指定數目。

DateTime(Int64, DateTimeKind)

DateTime 結構的新執行個體初始化為指定的刻度數以及國際標準時間 (UTC) 或本地時間。

欄位

MaxValue

代表 DateTime 最大的可能值。 此欄位為唯讀。

MinValue

代表 DateTime 最小的可能值。 此欄位為唯讀。

UnixEpoch

這個常數的值相當於西曆中的國際標準時間 00:00:00.0000000,1970 年 1 月 1 日。 UnixEpoch 定義 UNIX 時間等於 0 的時間點。

屬性

Date

取得這個執行個體的日期部分。

Day

取得由這個執行個體表示的月份天數。

DayOfWeek

取得由這個執行個體表示的一週天數。

DayOfYear

取得由這個執行個體表示的一年天數。

Hour

取得這個執行個體所表示日期的小時元件。

Kind

取得值,指出這個執行個體表示的時間是根據本地時間、國際標準時間 (UTC),或兩者皆非。

Microsecond

微秒元件,以介於 0 到 999 之間的值表示。

Millisecond

取得這個執行個體所表示日期的毫秒元件。

Minute

取得這個執行個體所表示日期的分鐘元件。

Month

取得這個執行個體所表示日期的月份元件。

Nanosecond

nanoseconds 元件,以介於 0 到 900 (之間的值表示,以 100 奈秒為單位遞增) 。

Now

取得 DateTime 物件,此物件會設定為這部電腦上目前的日期和時間,以本地時間表示。

Second

取得這個執行個體所表示日期的秒數元件。

Ticks

取得表示這個執行個體日期和時間的刻度數目。

TimeOfDay

取得這個執行個體的一天時間。

Today

取得目前的日期。

UtcNow

取得 DateTime 物件,此物件會設定為這部電腦上目前的日期和時間,以國際標準時間 (UTC) 表示。

Year

取得這個執行個體所表示日期的年份元件。

方法

Add(TimeSpan)

傳回新的 DateTime,將這個執行個體的值加上指定之 TimeSpan 的值。

AddDays(Double)

傳回新的 DateTime,將這個執行個體的值加上指定的天數。

AddHours(Double)

傳回新的 DateTime,將這個執行個體的值加上指定的時數。

AddMicroseconds(Double)

傳回新的 DateTime ,將指定的微秒數新增至這個實例的值。

AddMilliseconds(Double)

傳回新的 DateTime,將這個執行個體的值加上指定的毫秒數。

AddMinutes(Double)

傳回新的 DateTime,將這個執行個體的值加上指定的分鐘數。

AddMonths(Int32)

傳回新的 DateTime,將這個執行個體的值加上指定的月數。

AddSeconds(Double)

傳回新的 DateTime,將這個執行個體的值加上指定的秒數。

AddTicks(Int64)

傳回新的 DateTime,將這個執行個體的值加上指定的刻度數。

AddYears(Int32)

傳回新的 DateTime,將這個執行個體的值加上指定的年數。

Compare(DateTime, DateTime)

比較 DateTime 的兩個執行個體,並傳回整數,這個整數表示第一個執行個體早於、同於或晚於第二個執行個體。

CompareTo(DateTime)

比較這個執行個體的值與指定的 DateTime 值,並且傳回一個整數,指出這個執行個體是早於、同於或晚於指定的 DateTime 值。

CompareTo(Object)

比較這個執行個體的值與含有指定之 DateTime 值的指定物件,並且傳回一個整數,指出這個執行個體是早於、同於或晚於指定的 DateTime 值。

DaysInMonth(Int32, Int32)

傳回所指定年份中指定月份的天數。

Deconstruct(DateOnly, TimeOnly)

將這個 DateTime 實體解構為 DateOnlyTimeOnly

Deconstruct(Int32, Int32, Int32)

將這個 DateOnly 實體解構為 YearMonthDay

Equals(DateTime)

傳回值,該值指出這個執行個體的值是否等於指定之 DateTime 執行個體的值。

Equals(DateTime, DateTime)

傳回值,指出兩 DateTime 個實例是否有相同的日期和時間值。

Equals(Object)

傳回值,該值表示這個執行個體是否和指定的物件相等。

FromBinary(Int64)

還原序列化 64 位元的二進位值,並重新建立原始的序列化 DateTime 物件。

FromFileTime(Int64)

將指定的 Windows 檔案時間轉換成相等的本地時間。

FromFileTimeUtc(Int64)

將指定的 Windows 檔案時間轉換成相等的 UTC 時間。

FromOADate(Double)

傳回等於指定 OLE Automation 日期的 DateTime

GetDateTimeFormats()

將這個執行個體的值轉換為標準日期和時間格式規範所支援的所有字串表示。

GetDateTimeFormats(Char)

將這個執行個體的值轉換為指定的標準日期和時間格式規範所支援的所有字串表示。

GetDateTimeFormats(Char, IFormatProvider)

將這個執行個體的值轉換為指定的標準日期和時間格式規範和特定文化特性格式資訊所支援的所有字串表示。

GetDateTimeFormats(IFormatProvider)

將這個執行個體的值轉換為標準日期和時間格式規範和指定的特定文化特性格式資訊所支援的所有字串表示。

GetHashCode()

傳回這個執行個體的雜湊碼。

GetTypeCode()

傳回實值類型 TypeCodeDateTime

IsDaylightSavingTime()

指出這個 DateTime 執行個體是否在目前時區的日光節約時間範圍內。

IsLeapYear(Int32)

傳回指定年份是否為閏年的指示。

Parse(ReadOnlySpan<Char>, IFormatProvider)

將字元範圍剖析為值。

Parse(ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles)

使用文化特定格式資訊及格式化樣式,將包含日期與時間字串表示的記憶體範圍轉換成相等 DateTime

Parse(String)

使用目前文化特性的慣例,將日期和時間的字串表示轉換為相 DateTime 等的。

Parse(String, IFormatProvider)

使用特定文化特性格式資訊,將日期和時間的字串表示轉換為其相等的 DateTime

Parse(String, IFormatProvider, DateTimeStyles)

使用特定文化特性格式資訊及格式樣式,將日期和時間的字串表示轉換為其相等的 DateTime

ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles)

使用指定的格式、特定文化特性格式資訊以及樣式,以將日期和時間的指定範圍表示轉換為與其相等的 DateTime。 字串表示的格式必須完全符合指定的格式,否則會擲回例外狀況。

ParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, DateTimeStyles)

使用指定格式、特定文化特性格式資訊以及樣式的陣列,以將日期和時間的指定範圍表示轉換為與其相等的 DateTime。 字串表示的格式必須至少完全符合其中一個指定的格式,否則會擲回例外狀況。

ParseExact(String, String, IFormatProvider)

使用指定的格式以及特定文化特性的格式資訊,將日期和時間的指定字串表示,轉換為其相等的 DateTime。 字串表示的格式必須完全符合指定的格式。

ParseExact(String, String, IFormatProvider, DateTimeStyles)

使用指定的格式、特定文化特性格式資訊以及樣式,將日期和時間的指定字串表示,轉換為其相等的 DateTime。 字串表示的格式必須完全符合指定的格式,否則會擲回例外狀況。

ParseExact(String, String[], IFormatProvider, DateTimeStyles)

使用指定的格式陣列、特定文化特性格式資訊以及樣式,將日期和時間的指定字串表示,轉換為其相等的 DateTime。 字串表示的格式必須至少完全符合其中一個指定的格式,否則會擲回例外狀況。

SpecifyKind(DateTime, DateTimeKind)

建立新的 DateTime 物件,此物件的刻度數與指定的 DateTime 相同,但依指定的 DateTimeKind 值所示,指定為本地時間、國際標準時間 (Coordinated Universal Time,UTC),或兩者都不是。

Subtract(DateTime)

傳回會將指定日期和時間從此執行個體的值減去的新 TimeSpan

Subtract(TimeSpan)

傳回會將指定期間從此執行個體的值減去的新 DateTime

ToBinary()

將目前的 DateTime 物件序列化成 64 位元的二進位值,之後可以用這個值來重新建立 DateTime 物件。

ToFileTime()

將目前 DateTime 物件的值轉換成 Windows 檔案時間。

ToFileTimeUtc()

將目前 DateTime 物件的值轉換成 Windows 檔案時間。

ToLocalTime()

將目前 DateTime 物件的值轉換成本地時間。

ToLongDateString()

將目前 DateTime 物件的值轉換為其相等的完整日期 (Long Date) 字串表示。

ToLongTimeString()

將目前 DateTime 物件的值轉換為其相等的完整時間 (Long Time) 字串表示。

ToOADate()

將這個執行個體的值轉換為相等的 OLE Automation 日期。

ToShortDateString()

將目前 DateTime 物件的值轉換為其相等的簡短日期 (Short Date) 字串表示。

ToShortTimeString()

將目前 DateTime 物件的值轉換為其相等的簡短時間 (Short Time) 字串表示。

ToString()

使用目前文化特性的格式化慣例,將目前 DateTime 物件的值轉換為其相等的字串表示。

ToString(IFormatProvider)

使用指定的特定文化特性格式資訊,將目前 DateTime 物件的值轉換為其相等的字串表示。

ToString(String)

使用指定的格式和目前文化特性的格式化慣例,將目前 DateTime 物件的值轉換為其相等的字串表示。

ToString(String, IFormatProvider)

使用指定的格式和特定文化特性的格式資訊,將目前 DateTime 物件的值,轉換為其相等的字串表示。

ToUniversalTime()

將目前 DateTime 物件的值轉換成國際標準時間 (UTC)。

TryFormat(Span<Byte>, Int32, ReadOnlySpan<Char>, IFormatProvider)

嘗試將目前實例的值格式化為UTF-8到提供的位元組範圍。

TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>, IFormatProvider)

嘗試將目前日期時間執行個體的值格式化為所提供字元範圍。

TryParse(ReadOnlySpan<Char>, DateTime)

將日期和時間的指定字元範圍轉換為與其相等的 DateTime,並傳回一個值表示轉換是否成功。

TryParse(ReadOnlySpan<Char>, IFormatProvider, DateTime)

嘗試將字元範圍剖析成值。

TryParse(ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles, DateTime)

使用指定的特定文化特性格式資訊和格式樣式,以將日期和時間的範圍表示轉換為與其相等的 DateTime,並傳回一個值表示轉換是否成功。

TryParse(String, DateTime)

將日期和時間的指定字串表示,轉換為其相等的 DateTime,並傳回一個值表示轉換是否成功。

TryParse(String, IFormatProvider, DateTime)

嘗試將字串剖析成值。

TryParse(String, IFormatProvider, DateTimeStyles, DateTime)

使用指定的特定文化特性格式資訊和格式樣式,將日期和時間的指定字串表示轉換為其相等的 DateTime,並傳回值,這個值表示轉換是否成功。

TryParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles, DateTime)

使用指定的格式、特定文化特性格式資訊以及樣式,以將日期和時間的指定範圍表示轉換為與其相等的 DateTime。 字串表示的格式必須完全符合指定的格式。 此方法會傳回值,這個值表示轉換是否成功。

TryParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, DateTimeStyles, DateTime)

將日期和時間的指定字元範圍轉換為與其相等的 DateTime,並傳回一個值表示轉換是否成功。

TryParseExact(String, String, IFormatProvider, DateTimeStyles, DateTime)

使用指定的格式、特定文化特性格式資訊以及樣式,將日期和時間的指定字串表示,轉換為其相等的 DateTime。 字串表示的格式必須完全符合指定的格式。 此方法會傳回值,這個值表示轉換是否成功。

TryParseExact(String, String[], IFormatProvider, DateTimeStyles, DateTime)

使用指定的格式陣列、特定文化特性格式資訊以及樣式,將日期和時間的指定字串表示,轉換為其相等的 DateTime。 字串表示的格式必須至少完全符合其中一個指定格式。 此方法會傳回值,這個值表示轉換是否成功。

運算子

Addition(DateTime, TimeSpan)

將指定的日期和時間加上指定的時間間隔,產生新的日期和時間。

Equality(DateTime, DateTime)

判斷 DateTime 的兩個指定執行個體是否相等。

GreaterThan(DateTime, DateTime)

判斷某個指定的 DateTime 是否晚於另一個指定的 DateTime

GreaterThanOrEqual(DateTime, DateTime)

判斷某個指定之 DateTime 表示的是同於還是晚於另一個指定之 DateTime 的日期和時間。

Inequality(DateTime, DateTime)

判斷 DateTime 的兩個指定執行個體是否不相等。

LessThan(DateTime, DateTime)

判斷某個指定的 DateTime 是否早於另一個指定的 DateTime

LessThanOrEqual(DateTime, DateTime)

判斷某個指定之 DateTime 表示的是同於還是早於另一個指定之 DateTime 的日期和時間。

Subtraction(DateTime, DateTime)

將另一個指定的日期和時間減去指定的日期和時間,並傳回時間間隔。

Subtraction(DateTime, TimeSpan)

將指定的日期和時間減去指定的時間間隔,並傳回新的日期和時間。

明確介面實作

IComparable.CompareTo(Object)

將目前的執行個體與相同類型的另一個物件相比較,並傳回整數,這個整數表示目前的執行個體在排序次序中,位於另一個物件之前、之後或相同位置。

IConvertible.GetTypeCode()

傳回這個執行個體的 TypeCode

IConvertible.ToBoolean(IFormatProvider)

不支援此轉換。 嘗試使用這個方法會擲回 InvalidCastException

IConvertible.ToByte(IFormatProvider)

不支援此轉換。 嘗試使用這個方法會擲回 InvalidCastException

IConvertible.ToChar(IFormatProvider)

不支援此轉換。 嘗試使用這個方法會擲回 InvalidCastException

IConvertible.ToDateTime(IFormatProvider)

傳回目前的 DateTime物件。

IConvertible.ToDecimal(IFormatProvider)

不支援此轉換。 嘗試使用這個方法會擲回 InvalidCastException

IConvertible.ToDouble(IFormatProvider)

不支援此轉換。 嘗試使用這個方法會擲回 InvalidCastException

IConvertible.ToInt16(IFormatProvider)

不支援此轉換。 嘗試使用這個方法會擲回 InvalidCastException

IConvertible.ToInt32(IFormatProvider)

不支援此轉換。 嘗試使用這個方法會擲回 InvalidCastException

IConvertible.ToInt64(IFormatProvider)

不支援此轉換。 嘗試使用這個方法會擲回 InvalidCastException

IConvertible.ToSByte(IFormatProvider)

不支援此轉換。 嘗試使用這個方法會擲回 InvalidCastException

IConvertible.ToSingle(IFormatProvider)

不支援此轉換。 嘗試使用這個方法會擲回 InvalidCastException

IConvertible.ToType(Type, IFormatProvider)

將目前的 DateTime 物件轉換為指定類型的物件。

IConvertible.ToUInt16(IFormatProvider)

不支援此轉換。 嘗試使用這個方法會擲回 InvalidCastException

IConvertible.ToUInt32(IFormatProvider)

不支援此轉換。 嘗試使用這個方法會擲回 InvalidCastException

IConvertible.ToUInt64(IFormatProvider)

不支援此轉換。 嘗試使用這個方法會擲回 InvalidCastException

ISerializable.GetObjectData(SerializationInfo, StreamingContext)

使用序列化目前 SerializationInfo 物件所需的資料,填入 DateTime 物件。

適用於

執行緒安全性

此類型的所有成員都是安全線程。 看似修改實例狀態的成員,實際上會傳回以新值初始化的新實例。 如同任何其他類型,讀取和寫入包含此類型實例的共用變數必須受到鎖定保護,以確保線程安全。

另請參閱