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

获取或设置表示处于“上午”(中午前)的各小时的字符串指示符。

Calendar

获取或设置用于当前区域性的日历。

CalendarWeekRule

获取或设置一个值,该值指定使用哪个规则确定该年的第一个日历周。

CurrentInfo

获取基于当前区域性对值进行格式设置的只读的 DateTimeFormatInfo 对象。

DateSeparator

获取或设置分隔日期中各组成部分(即年、月、日)的字符串。

DayNames

获取或设置一维字符串数组,它包含该周中各天的特定于区域性的完整名称。

FirstDayOfWeek

获取或设置一周的第一天。

FullDateTimePattern

为长日期和长时间值获取或设置自定义格式字符串。

InvariantInfo

获取不依赖于区域性的(固定)默认只读的 DateTimeFormatInfo 对象。

IsReadOnly

获取一个值,该值指示 DateTimeFormatInfo 对象是否为只读。

LongDatePattern

获取或设置长日期值的自定义格式字符串。

LongTimePattern

为长时间值获取或设置自定义格式字符串。

MonthDayPattern

获取或设置月份和日期值的自定义格式字符串。

MonthGenitiveNames

获取或设置与当前 DateTimeFormatInfo 对象关联的月份名称的字符串数组。

MonthNames

获取或设置 String 类型的一维数组,它包含月份的区域性特定的全名。

NativeCalendarName

获取与当前 DateTimeFormatInfo 对象关联的本机日历名称。

PMDesignator

获取或设置表示处于“下午”(中午后)的各小时的字符串指示符。

RFC1123Pattern

获取自定义的格式字符串,该字符串用于基于 Internet 工程任务组 (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)

返回与指定 IFormatProvider 关联的 DateTimeFormatInfo 对象。

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。

适用于

另请参阅