DateTime 結構

定義

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

public value class DateTime : IComparable, IComparable<DateTime>, IConvertible, IEquatable<DateTime>, IFormattable
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, 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, 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>, IParsable<DateTime>, ISpanFormattable, ISpanParsable<DateTime>, IUtf8SpanFormattable, System.Runtime.Serialization.ISerializable
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 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 IParsable<DateTime>
    interface ISpanFormattable
    interface ISpanParsable<DateTime>
    interface IUtf8SpanFormattable
    interface ISerializable
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
[<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), IParsable(Of DateTime), ISerializable, ISpanFormattable, ISpanParsable(Of DateTime), IUtf8SpanFormattable
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, IConvertible, IFormattable
Public Structure DateTime
Implements IComparable, IComparable(Of DateTime), IEquatable(Of DateTime), IFormattable
繼承
DateTime
屬性
實作

備註

欲了解更多關於此 API 的資訊,請參閱 DateTime 的補充 API 備註

建構函式

名稱 Description
DateTime(DateOnly, TimeOnly, DateTimeKind)

將結構的新實例 DateTime 初始化為指定的 DateOnlyTimeOnly ,並尊重指定的 DateTimeKind

DateTime(DateOnly, TimeOnly)

將該結構的新實例 DateTime 初始化為指定的 DateOnlyTimeOnly。 新的實例會有那 Unspecified 種。

DateTime(Int32, Int32, Int32, Calendar)

初始化結構的新實例 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, Calendar, DateTimeKind)

將結構的新實例 DateTime 初始化為指定的年、月、日、時、分、秒、毫秒及協調世界時(UTC)或當地時間。

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

初始化結構的新實例 DateTime ,初始化為指定的年、月、日、時、分、秒及毫秒,適用於指定的曆法。

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

將結構的新實例 DateTime 初始化為指定的年份、月份、日、時、分、秒、毫秒及協調世界時(UTC)或當地時間。

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

將結構的新實例 DateTime 初始化為指定的年、月、日、時、分、秒、毫秒及協調世界時(UTC)或當地時間。

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

將結構的新實例 DateTime 初始化為指定的年、月、日、時、分、秒、毫秒及協調世界時(UTC)或當地時間。

DateTime(Int32, 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)

初始化結構的新實例 DateTime ,設定為指定的年份、月份、日、時、分、秒和毫秒。

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

初始化結構的新實例 DateTime ,設定為指定的年份、月份、日、時、分鐘和秒。

DateTime(Int32, Int32, Int32)

將結構的新實例 DateTime 初始化為指定的年份、月份和日期。

DateTime(Int64, DateTimeKind)

將結構的新實例 DateTime 初始化為指定的刻數,並初始化至協調世界時(UTC)或當地時間。

DateTime(Int64)

將結構的新實例 DateTime 初始化為指定的 tick 數。

欄位

名稱 Description
MaxValue

代表 的最大 DateTime可能值。 此欄位僅供讀取。

MinValue

代表最小 DateTime的可能值。 此欄位僅供讀取。

UnixEpoch

此常數的值相當於1970年1月1日(格里曆)00:00:00.00000000 UTC。 UnixEpoch 定義了 Unix 時間等於 0 的時間點。

屬性

名稱 Description
Date

取得這個實例的日期成分。

Day

用這個例子表示月份的星期幾。

DayOfWeek

會用這個例子代表星期幾。

DayOfYear

用這個例子表示年份的星期幾。

Hour

取得表示此實例之日期的時數成分。

Kind

會得到一個值,表示此實例所代表的時間是基於當地時間、協調世界時(UTC),還是兩者皆非。

Microsecond

微秒分量,表示介於0到999之間的值。

Millisecond

取得此實例所代表日期的毫秒成分。

Minute

取得此實例所表示日期的分鐘部分。

Month

獲取此實例所表示日期的月份元件。

Nanosecond

奈秒分量,以0到900(以100奈秒為單位)表示。

Now

取得 DateTime 一個物件,設定為本電腦的當前日期與時間,以當地時間表示。

Second

取得這個實例所表示日期的秒數部分。

Ticks

取得代表此實例日期與時間的勾數。

TimeOfDay

這次他有時間理會。

Today

拿到目前的日期。

UtcNow

取得 DateTime 一個設定為此電腦當前日期與時間的物件,表示為協調世界時(UTC)。

Year

取得這個實例所表示日期的年份元件。

方法

名稱 Description
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)

回傳一個值,表示兩個 DateTime 實例的日期與時間值是否相同。

Equals(DateTime)

回傳一個值,表示該實例的值是否等於指定 DateTime 實例的值。

Equals(Object)

傳回值,指出這個實例是否等於指定的物件。

FromBinary(Int64)

將 64 位元二進位值反序列化,並重建原始序列化 DateTime 物件。

FromFileTime(Int64)

將指定的 Windows 檔案時間轉換為等效的本地時間。

FromFileTimeUtc(Int64)

將指定的 Windows 檔案時間轉換為等效的 UTC 時間。

FromOADate(Double)

回傳 DateTime 相當於指定 OLE 自動化日期的資訊。

GetDateTimeFormats()

將此實例的值轉換為標準日期與時間格式規範符所支援的所有字串表示。

GetDateTimeFormats(Char, IFormatProvider)

將此實例的值轉換為指定標準日期與時間格式指定符及文化特定格式資訊所支援的所有字串表示。

GetDateTimeFormats(Char)

將此實例的值轉換為指定標準日期與時間格式指定符所支援的所有字串表示。

GetDateTimeFormats(IFormatProvider)

將此實例的值轉換為標準日期與時間格式指定符及特定文化格式資訊所支援的所有字串表示。

GetHashCode()

傳回這個實例的哈希碼。

GetTypeCode()

回傳 TypeCode 的值型別 DateTime

IsDaylightSavingTime()

顯示此實例 DateTime 是否屬於當前時區的夏令時間範圍內。

IsLeapYear(Int32)

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

Parse(ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles)

透過使用文化特定的格式資訊與格式風格,將包含日期與時間字串表示的記憶體範圍轉換為對 DateTime 應的。

Parse(ReadOnlySpan<Char>, IFormatProvider)

將字元範圍剖析為值。

Parse(String, IFormatProvider, DateTimeStyles)

利用特定文化的格式資訊與格式風格,將日期與時間的字串表示轉換為對 DateTime 應的。

Parse(String, IFormatProvider)

利用文化特定的格式資訊,將日期與時間的字串表示轉換為其 DateTime 等效值。

Parse(String)

利用當前文化的慣例,將日期與時間的字串表示轉換為其 DateTime 等價值。

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

將指定跨度的日期與時間 DateTime 表示轉換為其對應的格式、文化特定格式資訊及風格。 字串表示的格式必須完全符合指定格式,否則會拋出例外。

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

利用指定的格式陣列、文化特定格式資訊及樣式,將指定的日期與時間跨度表示轉換為對 DateTime 應的。 字串表示的格式必須至少符合其中一個指定格式,否則會拋出例外。

ParseExact(String, String, IFormatProvider, DateTimeStyles)

將指定的日期與時間 DateTime 字串表示轉換為其對應的格式、特定文化格式資訊及樣式。 字串表示的格式必須完全符合指定格式,否則會拋出例外。

ParseExact(String, String, IFormatProvider)

利用指定的格式及文化特定格式資訊,將指定的日期與時間字串表示轉換為其 DateTime 對應的日期與時間。 字串表示的格式必須完全符合指定的格式。

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

利用指定的格式陣列、文化特定格式資訊及樣式,將指定的日期與時間字串表示轉換為對 DateTime 應的。 字串表示的格式必須至少符合其中一個指定格式,否則會拋出例外。

SpecifyKind(DateTime, DateTimeKind)

建立 DateTime 一個新物件,其刻數與指定的 DateTime相同,但根據指定 DateTimeKind 值,指定為當地時間、協調世界時(UTC)或兩者皆非。

Subtract(DateTime)

回傳一個新資料 TimeSpan ,從該實例的值中減去指定的日期和時間。

Subtract(TimeSpan)

回傳一個新值 DateTime ,從該實例的值中減去指定的持續時間。

ToBinary()

將目前 DateTime 物件序列化為 64 位元二進位值,之後可用來重建該 DateTime 物件。

ToFileTime()

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

ToFileTimeUtc()

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

ToLocalTime()

將當前 DateTime 物件的值轉換為當地時間。

ToLongDateString()

將目前 DateTime 物件的值轉換為其等效的長日期字串表示。

ToLongTimeString()

將當前 DateTime 物件的值轉換為其等效的長時字串表示。

ToOADate()

將此實例的值轉換為等效的 OLE 自動化日期。

ToShortDateString()

將目前 DateTime 物件的值轉換為其等效的短日期字串表示。

ToShortTimeString()

將目前 DateTime 物件的值轉換為其等效的短時間字串表示。

ToString()

利用當前文化的格式慣例,將當前 DateTime 物件的值轉換為其等效的字串表示。

ToString(IFormatProvider)

利用指定的文化特有格式資訊,將當前 DateTime 物件的值轉換為其等效的字串表示。

ToString(String, IFormatProvider)

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

ToString(String)

將當前 DateTime 物件的值轉換為其等效字串表示,並依照指定格式及當前文化的格式慣例。

ToUniversalTime()

將當前 DateTime 物體的值轉換為協調世界時(UTC)。

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

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

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

嘗試將當前 datetime 實例的值格式化為提供的字元區間。

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 應的。 字串表示的格式必須完全符合其中一個指定的格式。 方法會傳回值,指出轉換是否成功。

操作員

名稱 Description
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)

從指定日期與時間減去指定時間區間,並回傳一個新的日期與時間。

明確介面實作

名稱 Description
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 物件所需的資料。

適用於

執行緒安全性

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

另請參閱