DateTimeFormatInfo 類別

定義

提供關於日期和時間值格式的特定文化特性資訊。

public ref class DateTimeFormatInfo sealed : IFormatProvider
public ref class DateTimeFormatInfo sealed : ICloneable, IFormatProvider
public ref class DateTimeFormatInfo sealed : ICloneable, IFormatProvider, System::Runtime::Serialization::ISerializable
public sealed class DateTimeFormatInfo : IFormatProvider
public sealed class DateTimeFormatInfo : ICloneable, IFormatProvider
[System.Serializable]
public sealed class DateTimeFormatInfo : ICloneable, IFormatProvider, System.Runtime.Serialization.ISerializable
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class DateTimeFormatInfo : ICloneable, IFormatProvider
type DateTimeFormatInfo = class
    interface IFormatProvider
type DateTimeFormatInfo = class
    interface ICloneable
    interface IFormatProvider
[<System.Serializable>]
type DateTimeFormatInfo = class
    interface ICloneable
    interface IFormatProvider
    interface ISerializable
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type DateTimeFormatInfo = class
    interface ICloneable
    interface IFormatProvider
Public NotInheritable Class DateTimeFormatInfo
Implements IFormatProvider
Public NotInheritable Class DateTimeFormatInfo
Implements ICloneable, IFormatProvider
Public NotInheritable Class DateTimeFormatInfo
Implements ICloneable, IFormatProvider, ISerializable
繼承
DateTimeFormatInfo
屬性
實作

範例

下列範例會使用反映來取得英文 (美國) 文化特性DateTimeFormatInfo之 對象的屬性。 它會顯示包含自定義格式字串的屬性值,並使用這些字串來顯示格式化日期。

using System;
using System.Globalization;
using System.Reflection;

public class Example
{
   public static void Main()
   {
      // Get the properties of an en-US DateTimeFormatInfo object.
      DateTimeFormatInfo dtfi = CultureInfo.GetCultureInfo("en-US").DateTimeFormat;
      Type typ = dtfi.GetType();
      PropertyInfo[] props = typ.GetProperties();
      DateTime value = new DateTime(2012, 5, 28, 11, 35, 0);

      foreach (var prop in props) {
         // Is this a format pattern-related property?
         if (prop.Name.Contains("Pattern")) {
            string fmt = prop.GetValue(dtfi, null).ToString();
            Console.WriteLine("{0,-33} {1} \n{2,-37}Example: {3}\n",
                              prop.Name + ":", fmt, "",
                              value.ToString(fmt));
         }
      }
   }
}
// The example displays the following output:
//    FullDateTimePattern:              dddd, MMMM dd, yyyy h:mm:ss tt
//                                         Example: Monday, May 28, 2012 11:35:00 AM
//
//    LongDatePattern:                  dddd, MMMM dd, yyyy
//                                         Example: Monday, May 28, 2012
//
//    LongTimePattern:                  h:mm:ss tt
//                                         Example: 11:35:00 AM
//
//    MonthDayPattern:                  MMMM dd
//                                         Example: May 28
//
//    RFC1123Pattern:                   ddd, dd MMM yyyy HH':'mm':'ss 'GMT'
//                                         Example: Mon, 28 May 2012 11:35:00 GMT
//
//    ShortDatePattern:                 M/d/yyyy
//                                         Example: 5/28/2012
//
//    ShortTimePattern:                 h:mm tt
//                                         Example: 11:35 AM
//
//    SortableDateTimePattern:          yyyy'-'MM'-'dd'T'HH':'mm':'ss
//                                         Example: 2012-05-28T11:35:00
//
//    UniversalSortableDateTimePattern: yyyy'-'MM'-'dd HH':'mm':'ss'Z'
//                                         Example: 2012-05-28 11:35:00Z
//
//    YearMonthPattern:                 MMMM, yyyy
//                                         Example: May, 2012
Imports System.Globalization
Imports System.Reflection

Module Example
   Public Sub Main()
      ' Get the properties of an en-US DateTimeFormatInfo object.
      Dim dtfi As DateTimeFormatInfo = CultureInfo.GetCultureInfo("en-US").DateTimeFormat
      Dim typ As Type = dtfi.GetType()
      Dim props() As PropertyInfo = typ.GetProperties()
      Dim value As Date = #05/28/2012 11:35AM# 
      
      For Each prop In props
         ' Is this a format pattern-related property?
         If prop.Name.Contains("Pattern") Then
            Dim fmt As String = CStr(prop.GetValue(dtfi, Nothing))
            Console.WriteLine("{0,-33} {1} {2}{3,-37}Example: {4}", 
                              prop.Name + ":", fmt, vbCrLf, "",
                              value.ToString(fmt)) 
            Console.WriteLine()
         End If
      Next
   End Sub
End Module
' The example displays the following output:
'    FullDateTimePattern:              dddd, MMMM dd, yyyy h:mm:ss tt
'                                         Example: Monday, May 28, 2012 11:35:00 AM
'    
'    LongDatePattern:                  dddd, MMMM dd, yyyy
'                                         Example: Monday, May 28, 2012
'    
'    LongTimePattern:                  h:mm:ss tt
'                                         Example: 11:35:00 AM
'    
'    MonthDayPattern:                  MMMM dd
'                                         Example: May 28
'    
'    RFC1123Pattern:                   ddd, dd MMM yyyy HH':'mm':'ss 'GMT'
'                                         Example: Mon, 28 May 2012 11:35:00 GMT
'    
'    ShortDatePattern:                 M/d/yyyy
'                                         Example: 5/28/2012
'    
'    ShortTimePattern:                 h:mm tt
'                                         Example: 11:35 AM
'    
'    SortableDateTimePattern:          yyyy'-'MM'-'dd'T'HH':'mm':'ss
'                                         Example: 2012-05-28T11:35:00
'    
'    UniversalSortableDateTimePattern: yyyy'-'MM'-'dd HH':'mm':'ss'Z'
'                                         Example: 2012-05-28 11:35:00Z
'    
'    YearMonthPattern:                 MMMM, yyyy
'                                         Example: May, 2012

備註

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

建構函式

DateTimeFormatInfo()

初始化與文化特性無關 (非變異) 之 DateTimeFormatInfo 類別的可寫入新執行個體。

屬性

AbbreviatedDayNames

取得或設定包含特定文化特性之星期縮寫名稱的一維陣列 (型別為 String)。

AbbreviatedMonthGenitiveNames

取得或設定字串陣列,這個陣列包含與目前 DateTimeFormatInfo 物件關聯的縮寫月份名稱。

AbbreviatedMonthNames

取得或設定包含特定文化特性之月份縮寫名稱的一維字串陣列。

AMDesignator

取得或設定 "ante meridiem" (正午以前) 小時的字串指示項。

Calendar

取得或設定目前文化特性所使用的曆法。

CalendarWeekRule

取得或設定數值,指定要使用哪一個規則 (Rule) 來決定一年中的第一個日曆週。

CurrentInfo

取得根據目前文化特性格式化值的唯讀 DateTimeFormatInfo 物件。

DateSeparator

取得或設定分隔日期組成部分 (即年、月和日) 的字串。

DayNames

取得或設定包含特定文化特性之一週日期完整名稱的一維字串陣列。

FirstDayOfWeek

取得或設定一週的第一天。

FullDateTimePattern

取得或設定完整日期和時間值的自訂格式字串。

InvariantInfo

取得與文化特性無關 (非變異) 的預設唯讀 DateTimeFormatInfo 物件。

IsReadOnly

取得值,這個值表示 DateTimeFormatInfo 物件是否唯讀。

LongDatePattern

取得或設定完整日期值的自訂格式字串。

LongTimePattern

取得或設定完整時間值的自訂格式字串。

MonthDayPattern

取得或設定月份和日值的自訂格式字串。

MonthGenitiveNames

取得或設定字串陣列,這個陣列包含與目前 DateTimeFormatInfo 物件關聯的月份名稱。

MonthNames

取得或設定包含特定文化特性之月份完整名稱的一維陣列 (型別為 String)。

NativeCalendarName

取得行事曆的原生 (Native) 名稱,這個行事曆與目前 DateTimeFormatInfo 物件關聯。

PMDesignator

取得或設定 "post meridiem" (正午以後) 小時的字串指示項。

RFC1123Pattern

取得時間值的自訂格式字串,這個字串是根據網際網路工程任務推動小組 (Internet Engineering Task Force,IETF) 要求建議 (RFC) 1123 規格。

ShortDatePattern

取得或設定簡短日期值的自訂格式字串。

ShortestDayNames

取得或設定字串陣列,這個陣列包含與目前 DateTimeFormatInfo 物件關聯的最短唯一縮寫日名稱。

ShortTimePattern

取得或設定簡短時間值的自訂格式字串。

SortableDateTimePattern

取得可排序日期和時間值的自訂格式字串。

TimeSeparator

取得或設定分隔時間組成部分 (即時、分和秒) 的字串。

UniversalSortableDateTimePattern

取得自訂格式字串,以取得由 ISO 8601 所定義的國際標準、可排序日期和時間字串。

YearMonthPattern

取得或設定年份和月份值的自訂格式字串。

方法

Clone()

建立 DateTimeFormatInfo 的淺層複本。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetAbbreviatedDayName(DayOfWeek)

根據與目前 DateTimeFormatInfo 物件關聯的文化特性,傳回一星期內指定某一天的文化特性特有縮寫名稱。

GetAbbreviatedEraName(Int32)

如果縮寫存在,傳回含有指定紀元縮寫名稱的字串。

GetAbbreviatedMonthName(Int32)

根據與目前 DateTimeFormatInfo 物件關聯的文化特性,傳回指定月份的文化特性特有縮寫名稱。

GetAllDateTimePatterns()

傳回可格式化日期和時間值的所有標準模式。

GetAllDateTimePatterns(Char)

傳回可以使用所指定標準格式字串來格式化日期和時間值的所有模式。

GetDayName(DayOfWeek)

根據與目前 DateTimeFormatInfo 物件關聯的文化特性,傳回一星期內指定某一天的文化特性特有完整名稱。

GetEra(String)

傳回代表指定紀元的整數。

GetEraName(Int32)

傳回含有指定紀元名稱的字串。

GetFormat(Type)

傳回指定型別的對象,這個物件會提供日期和時間格式設定服務。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetInstance(IFormatProvider)

傳回與指定 DateTimeFormatInfo 關聯的 IFormatProvider 物件。

GetMonthName(Int32)

根據與目前 DateTimeFormatInfo 物件關聯的文化特性,傳回指定月份的文化特性特有完整名稱。

GetShortestDayName(DayOfWeek)

為與目前 DateTimeFormatInfo 物件關聯的星期內指定的某一天,取得最短的縮寫日名稱。

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ReadOnly(DateTimeFormatInfo)

傳回唯讀的 DateTimeFormatInfo 包裝函式。

SetAllDateTimePatterns(String[], Char)

設定對應於指定之標準格式字串的自訂日期和時間格式字串。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

ISerializable.GetObjectData(SerializationInfo, StreamingContext)

將序列化目標物件所需的資料填入 SerializationInfo 中。

適用於

另請參閱