Breyta

Deila með


DateTimeFormatInfo Class

Definition

Provides culture-specific information about the format of date and time values.

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
Inheritance
DateTimeFormatInfo
Attributes
Implements

Examples

The following example uses reflection to get the properties of the DateTimeFormatInfo object for the English (United States) culture. It displays the value of those properties that contain custom format strings and uses those strings to display formatted dates.

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

Remarks

For more information about this API, see Supplemental API remarks for DateTimeFormatInfo.

Constructors

DateTimeFormatInfo()

Initializes a new writable instance of the DateTimeFormatInfo class that is culture-independent (invariant).

Properties

AbbreviatedDayNames

Gets or sets a one-dimensional array of type String containing the culture-specific abbreviated names of the days of the week.

AbbreviatedMonthGenitiveNames

Gets or sets a string array of abbreviated month names associated with the current DateTimeFormatInfo object.

AbbreviatedMonthNames

Gets or sets a one-dimensional string array that contains the culture-specific abbreviated names of the months.

AMDesignator

Gets or sets the string designator for hours that are "ante meridiem" (before noon).

Calendar

Gets or sets the calendar to use for the current culture.

CalendarWeekRule

Gets or sets a value that specifies which rule is used to determine the first calendar week of the year.

CurrentInfo

Gets a read-only DateTimeFormatInfo object that formats values based on the current culture.

DateSeparator

Gets or sets the string that separates the components of a date, that is, the year, month, and day.

DayNames

Gets or sets a one-dimensional string array that contains the culture-specific full names of the days of the week.

FirstDayOfWeek

Gets or sets the first day of the week.

FullDateTimePattern

Gets or sets the custom format string for a long date and long time value.

InvariantInfo

Gets the default read-only DateTimeFormatInfo object that is culture-independent (invariant).

IsReadOnly

Gets a value indicating whether the DateTimeFormatInfo object is read-only.

LongDatePattern

Gets or sets the custom format string for a long date value.

LongTimePattern

Gets or sets the custom format string for a long time value.

MonthDayPattern

Gets or sets the custom format string for a month and day value.

MonthGenitiveNames

Gets or sets a string array of month names associated with the current DateTimeFormatInfo object.

MonthNames

Gets or sets a one-dimensional array of type String containing the culture-specific full names of the months.

NativeCalendarName

Gets the native name of the calendar associated with the current DateTimeFormatInfo object.

PMDesignator

Gets or sets the string designator for hours that are "post meridiem" (after noon).

RFC1123Pattern

Gets the custom format string for a time value that is based on the Internet Engineering Task Force (IETF) Request for Comments (RFC) 1123 specification.

ShortDatePattern

Gets or sets the custom format string for a short date value.

ShortestDayNames

Gets or sets a string array of the shortest unique abbreviated day names associated with the current DateTimeFormatInfo object.

ShortTimePattern

Gets or sets the custom format string for a short time value.

SortableDateTimePattern

Gets the custom format string for a sortable date and time value.

TimeSeparator

Gets or sets the string that separates the components of time, that is, the hour, minutes, and seconds.

UniversalSortableDateTimePattern

Gets the custom format string for a universal, sortable date and time string, as defined by ISO 8601.

YearMonthPattern

Gets or sets the custom format string for a year and month value.

Methods

Clone()

Creates a shallow copy of the DateTimeFormatInfo.

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetAbbreviatedDayName(DayOfWeek)

Returns the culture-specific abbreviated name of the specified day of the week based on the culture associated with the current DateTimeFormatInfo object.

GetAbbreviatedEraName(Int32)

Returns the string containing the abbreviated name of the specified era, if an abbreviation exists.

GetAbbreviatedMonthName(Int32)

Returns the culture-specific abbreviated name of the specified month based on the culture associated with the current DateTimeFormatInfo object.

GetAllDateTimePatterns()

Returns all the standard patterns in which date and time values can be formatted.

GetAllDateTimePatterns(Char)

Returns all the patterns in which date and time values can be formatted using the specified standard format string.

GetDayName(DayOfWeek)

Returns the culture-specific full name of the specified day of the week based on the culture associated with the current DateTimeFormatInfo object.

GetEra(String)

Returns the integer representing the specified era.

GetEraName(Int32)

Returns the string containing the name of the specified era.

GetFormat(Type)

Returns an object of the specified type that provides a date and time formatting service.

GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetInstance(IFormatProvider)

Returns the DateTimeFormatInfo object associated with the specified IFormatProvider.

GetMonthName(Int32)

Returns the culture-specific full name of the specified month based on the culture associated with the current DateTimeFormatInfo object.

GetShortestDayName(DayOfWeek)

Obtains the shortest abbreviated day name for a specified day of the week associated with the current DateTimeFormatInfo object.

GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ReadOnly(DateTimeFormatInfo)

Returns a read-only DateTimeFormatInfo wrapper.

SetAllDateTimePatterns(String[], Char)

Sets the custom date and time format strings that correspond to a specified standard format string.

ToString()

Returns a string that represents the current object.

(Inherited from Object)

Explicit Interface Implementations

ISerializable.GetObjectData(SerializationInfo, StreamingContext)

Populates a SerializationInfo with the data needed to serialize the target object.

Applies to

See also