当前位置: 首页>>代码示例>>C#>>正文


C# DateTimeFormatInfo类代码示例

本文整理汇总了C#中System.Globalization.DateTimeFormatInfo的典型用法代码示例。如果您正苦于以下问题:C# DateTimeFormatInfo类的具体用法?C# DateTimeFormatInfo怎么用?C# DateTimeFormatInfo使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


DateTimeFormatInfo类属于System.Globalization命名空间,在下文中一共展示了DateTimeFormatInfo类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

//引入命名空间
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)); 
         }
      }
   }
}
开发者ID:.NET开发者,项目名称:System.Globalization,代码行数:26,代码来源:DateTimeFormatInfo

输出:

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

示例2:

System.Globalization.DateTimeFormatInfo dtfi; 

dtfi = System.Globalization.DateTimeFormatInfo.InvariantInfo;
Console.WriteLine(dtfi.IsReadOnly);               

dtfi = new System.Globalization.DateTimeFormatInfo();
Console.WriteLine(dtfi.IsReadOnly);               

dtfi = System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat;
Console.WriteLine(dtfi.IsReadOnly);
开发者ID:.NET开发者,项目名称:System.Globalization,代码行数:10,代码来源:DateTimeFormatInfo

输出:

True
False
True

示例3:

System.Globalization.CultureInfo ci = null;
System.Globalization.DateTimeFormatInfo dtfi = null;

// Instantiate a culture using CreateSpecificCulture.
ci = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
dtfi = ci.DateTimeFormat;
Console.WriteLine("{0} from CreateSpecificCulture: {1}", ci.Name, dtfi.IsReadOnly);

// Instantiate a culture using the CultureInfo constructor.
ci = new System.Globalization.CultureInfo("en-CA"); 
dtfi = ci.DateTimeFormat;
Console.WriteLine("{0} from CultureInfo constructor: {1}", ci.Name, dtfi.IsReadOnly);

// Retrieve a culture by calling the GetCultureInfo method.
ci = System.Globalization.CultureInfo.GetCultureInfo("en-AU");
dtfi = ci.DateTimeFormat;
Console.WriteLine("{0} from GetCultureInfo: {1}", ci.Name, dtfi.IsReadOnly);

// Instantiate a DateTimeFormatInfo object by calling DateTimeFormatInfo.GetInstance.  
ci = System.Globalization.CultureInfo.CreateSpecificCulture("en-GB");
dtfi = System.Globalization.DateTimeFormatInfo.GetInstance(ci);
Console.WriteLine("{0} from GetInstance: {1}", ci.Name, dtfi.IsReadOnly);
开发者ID:.NET开发者,项目名称:System.Globalization,代码行数:22,代码来源:DateTimeFormatInfo

输出:

en-US from CreateSpecificCulture: False
en-CA from CultureInfo constructor: False
en-AU from GetCultureInfo: True
en-GB from GetInstance: False

示例4:

System.Globalization.CultureInfo specific, neutral;
 System.Globalization.DateTimeFormatInfo dtfi;

 // Instantiate a culture by creating a specific culture and using its Parent property.
 specific = System.Globalization.CultureInfo.GetCultureInfo("fr-FR");
 neutral = specific.Parent;
 dtfi = neutral.DateTimeFormat;
 Console.WriteLine("{0} from Parent property: {1}", neutral.Name, dtfi.IsReadOnly);
 
 dtfi = System.Globalization.CultureInfo.GetCultureInfo("fr-FR").Parent.DateTimeFormat;
 Console.WriteLine("{0} from Parent property: {1}", neutral.Name, dtfi.IsReadOnly);

 // Instantiate a neutral culture using the CultureInfo constructor.
 neutral = new System.Globalization.CultureInfo("fr");
 dtfi = neutral.DateTimeFormat;
 Console.WriteLine("{0} from CultureInfo constructor: {1}", neutral.Name, dtfi.IsReadOnly);
 
 // Instantiate a culture using CreateSpecificCulture. 
 neutral = System.Globalization.CultureInfo.CreateSpecificCulture("fr");
 dtfi = neutral.DateTimeFormat;
 Console.WriteLine("{0} from CreateSpecificCulture: {1}", neutral.Name, dtfi.IsReadOnly);
 
 // Retrieve a culture by calling the GetCultureInfo method.
 neutral = System.Globalization.CultureInfo.GetCultureInfo("fr");
 dtfi = neutral.DateTimeFormat;
 Console.WriteLine("{0} from GetCultureInfo: {1}", neutral.Name, dtfi.IsReadOnly);

 // Instantiate a DateTimeFormatInfo object by calling GetInstance.  
 neutral = System.Globalization.CultureInfo.CreateSpecificCulture("fr");
 dtfi = System.Globalization.DateTimeFormatInfo.GetInstance(neutral);
 Console.WriteLine("{0} from GetInstance: {1}", neutral.Name, dtfi.IsReadOnly);

 // The example displays the following output:
 //       fr from Parent property: False
 //       fr from Parent property: False
 //       fr from CultureInfo constructor: False
 //       fr-FR from CreateSpecificCulture: False
 //       fr from GetCultureInfo: True
 //       fr-FR from GetInstance: False
开发者ID:.NET开发者,项目名称:System.Globalization,代码行数:39,代码来源:DateTimeFormatInfo

示例5: Main

//引入命名空间
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;

public class Example
{
   public static void Main()
   {
      // Get all the neutral cultures
      List<String> names = new List<String>();
      Array.ForEach(CultureInfo.GetCultures(CultureTypes.NeutralCultures),
                    culture => names.Add(culture.Name));
      names.Sort();
      foreach (var name in names) {
         // Ignore the invariant culture.
         if (name == "") continue;
         
         ListSimilarChildCultures(name);        
      }
   }

   private static void ListSimilarChildCultures(String name)
   {
      // Create the neutral DateTimeFormatInfo object.
      DateTimeFormatInfo dtfi = CultureInfo.GetCultureInfo(name).DateTimeFormat;
      // Retrieve all specific cultures of the neutral culture.
      CultureInfo[] cultures = Array.FindAll(CultureInfo.GetCultures(CultureTypes.SpecificCultures), 
                               culture => culture.Name.StartsWith(name + "-", StringComparison.OrdinalIgnoreCase));
      // Create an array of DateTimeFormatInfo properties
      PropertyInfo[] properties = typeof(DateTimeFormatInfo).GetProperties(BindingFlags.Instance | BindingFlags.Public);
      bool hasOneMatch = false;

      foreach (var ci in cultures) {
         bool match = true;     
         // Get the DateTimeFormatInfo for a specific culture.
         DateTimeFormatInfo specificDtfi = ci.DateTimeFormat;
         // Compare the property values of the two.
         foreach (var prop in properties) {
            // We're not interested in the value of IsReadOnly.     
            if (prop.Name == "IsReadOnly") continue;
            
            // For arrays, iterate the individual elements to see if they are the same.
            if (prop.PropertyType.IsArray) { 
               IList nList = (IList) prop.GetValue(dtfi, null);
               IList sList = (IList) prop.GetValue(specificDtfi, null);
               if (nList.Count != sList.Count) {
                  match = false;
Console.WriteLine("   Different n in {2} array for {0} and {1}", name, ci.Name, prop.Name);
                  break;
               } 

               for (int ctr = 0; ctr < nList.Count; ctr++) {
                  if (! nList[ctr].Equals(sList[ctr])) { 
                     match = false;
Console.WriteLine("   {0} value different for {1} and {2}", prop.Name, name, ci.Name);                     
                     break;
                  }     
               }
               
               if (! match) break;
            }
            // Get non-array values.
            else {
               Object specificValue = prop.GetValue(specificDtfi);
               Object neutralValue = prop.GetValue(dtfi);
                               
               // Handle comparison of Calendar objects.
               if (prop.Name == "Calendar") { 
                  // The cultures have a different calendar type.
                  if (specificValue.ToString() != neutralValue.ToString()) {
Console.WriteLine("   Different calendar types for {0} and {1}", name, ci.Name);
                     match = false;
                     break;
                  }
                   
                  if (specificValue is GregorianCalendar) {
                     if (((GregorianCalendar) specificValue).CalendarType != ((GregorianCalendar) neutralValue).CalendarType) {
Console.WriteLine("   Different Gregorian calendar types for {0} and {1}", name, ci.Name);
                        match = false;
                        break;
                     }
                  }
               }
               else if (! specificValue.Equals(neutralValue)) {
                  match = false;
Console.WriteLine("   Different {0} values for {1} and {2}", prop.Name, name, ci.Name);                  
                  break;   
               }
            }        
         }
         if (match) {
            Console.WriteLine("DateTimeFormatInfo object for '{0}' matches '{1}'", 
                              name, ci.Name);
            hasOneMatch = true;
         }                                       
      }
      if (! hasOneMatch)
         Console.WriteLine("DateTimeFormatInfo object for '{0}' --> No Match", name);            

      Console.WriteLine();
   }
}
开发者ID:.NET开发者,项目名称:System.Globalization,代码行数:105,代码来源:DateTimeFormatInfo

示例6:

DateTimeFormatInfo dtfi;

dtfi = DateTimeFormatInfo.CurrentInfo;
Console.WriteLine(dtfi.IsReadOnly);

dtfi = CultureInfo.CurrentCulture.DateTimeFormat;
Console.WriteLine(dtfi.IsReadOnly);

dtfi = DateTimeFormatInfo.GetInstance(CultureInfo.CurrentCulture);
Console.WriteLine(dtfi.IsReadOnly);
开发者ID:.NET开发者,项目名称:System.Globalization,代码行数:10,代码来源:DateTimeFormatInfo

输出:

True
True
True

示例7: Main

//引入命名空间
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      DateTimeFormatInfo current1 = DateTimeFormatInfo.CurrentInfo;
      current1 = (DateTimeFormatInfo) current1.Clone();
      Console.WriteLine(current1.IsReadOnly);

      CultureInfo culture2 = CultureInfo.CreateSpecificCulture(CultureInfo.CurrentCulture.Name);
      DateTimeFormatInfo current2 = culture2.DateTimeFormat;
      Console.WriteLine(current2.IsReadOnly);
   }
}
开发者ID:.NET开发者,项目名称:System.Globalization,代码行数:17,代码来源:DateTimeFormatInfo

输出:

False
False

示例8: Main

//引入命名空间
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      CultureInfo culture;
      DateTimeFormatInfo dtfi;
      
      culture = CultureInfo.CurrentCulture;
      dtfi = culture.DateTimeFormat;
      Console.WriteLine("Culture Name:      {0}", culture.Name);
      Console.WriteLine("User Overrides:    {0}", culture.UseUserOverride);
      Console.WriteLine("Long Time Pattern: {0}\n", culture.DateTimeFormat.LongTimePattern);
            
      culture = new CultureInfo(CultureInfo.CurrentCulture.Name, false);
      Console.WriteLine("Culture Name:      {0}",   culture.Name);
      Console.WriteLine("User Overrides:    {0}",   culture.UseUserOverride);
      Console.WriteLine("Long Time Pattern: {0}\n", culture.DateTimeFormat.LongTimePattern);
   }
}
开发者ID:.NET开发者,项目名称:System.Globalization,代码行数:23,代码来源:DateTimeFormatInfo

输出:

Culture Name:      en-US
User Overrides:    True
Long Time Pattern: HH:mm:ss

Culture Name:      en-US
User Overrides:    False
Long Time Pattern: h:mm:ss tt

示例9: GetFormat

//引入命名空间
using System;
using System.Globalization;

public class CurrentCultureFormatProvider : IFormatProvider
{
   public Object GetFormat(Type formatType) 
   {
      Console.WriteLine("Requesting an object of type {0}", 
                        formatType.Name);
      if (formatType == typeof(NumberFormatInfo))
         return NumberFormatInfo.CurrentInfo;
      else if (formatType == typeof(DateTimeFormatInfo))
         return DateTimeFormatInfo.CurrentInfo;
      else
         return null;
   }
}

public class Example
{
   public static void Main()
   {
      DateTime dateValue = new DateTime(2013, 5, 28, 12, 30, 0);
      string value = dateValue.ToString("F", new CurrentCultureFormatProvider());
      Console.WriteLine(value);
      Console.WriteLine();
      string composite = String.Format(new CurrentCultureFormatProvider(), 
                                       "Date: {0:d}   Amount: {1:C}   Description: {2}",
                                       dateValue, 1264.03m, "Service Charge");
      Console.WriteLine(composite);
      Console.WriteLine();
   }
}
开发者ID:.NET开发者,项目名称:System.Globalization,代码行数:34,代码来源:DateTimeFormatInfo

输出:

Requesting an object of type DateTimeFormatInfo
Tuesday, May 28, 2013 1:30:00 PM

Requesting an object of type ICustomFormatter
Requesting an object of type DateTimeFormatInfo
Requesting an object of type NumberFormatInfo
Date: 5/28/2013   Amount: $1,264.03   Description: Service Charge

示例10: Main

//引入命名空间
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      DateTime dateValue = new DateTime(2013, 8, 18); 
      CultureInfo enUS = CultureInfo.CreateSpecificCulture("en-US");
      DateTimeFormatInfo dtfi = enUS.DateTimeFormat;
      
      Console.WriteLine("Before modifying DateTimeFormatInfo object: ");
      Console.WriteLine("{0}: {1}\n", dtfi.ShortDatePattern, 
                                    dateValue.ToString("d", enUS));

      // Modify the short date pattern.
      dtfi.ShortDatePattern = "yyyy-MM-dd";
      Console.WriteLine("After modifying DateTimeFormatInfo object: ");
      Console.WriteLine("{0}: {1}", dtfi.ShortDatePattern, 
                                    dateValue.ToString("d", enUS));
   }
}
开发者ID:.NET开发者,项目名称:System.Globalization,代码行数:23,代码来源:DateTimeFormatInfo

输出:

Before modifying DateTimeFormatInfo object:
M/d/yyyy: 8/18/2013

After modifying DateTimeFormatInfo object:
yyyy-MM-dd: 2013-08-18

示例11: Main

//引入命名空间
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      DateTime dateValue = new DateTime(2013, 08, 28); 
      CultureInfo frFR = CultureInfo.CreateSpecificCulture("fr-FR");
      DateTimeFormatInfo dtfi = frFR.DateTimeFormat;
      
      Console.WriteLine("Before modifying DateSeparator property: {0}",
                        dateValue.ToString("g", frFR));
      
      // Modify the date separator.
      dtfi.DateSeparator = "-";
      Console.WriteLine("After modifying the DateSeparator property: {0}",
                        dateValue.ToString("g", frFR));
   }
}
开发者ID:.NET开发者,项目名称:System.Globalization,代码行数:21,代码来源:DateTimeFormatInfo

输出:

Before modifying DateSeparator property: 18/08/2013 00:00
After modifying the DateSeparator property: 18-08-2013 00:00

示例12: Main

//引入命名空间
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      DateTime value = new DateTime(2013, 7, 9);
      CultureInfo enUS = CultureInfo.CreateSpecificCulture("en-US");
      DateTimeFormatInfo dtfi = enUS.DateTimeFormat;
      String[] formats = { "D", "F", "f" };

      // Display date before modifying properties.
      foreach (var fmt in formats)
         Console.WriteLine("{0}: {1}", fmt, value.ToString(fmt, dtfi));

      Console.WriteLine();
      
      // We don't want to change the FullDateTimePattern, so we need to save it.
      String originalFullDateTimePattern = dtfi.FullDateTimePattern;
      
      // Modify day name abbreviations and long date pattern.
      dtfi.AbbreviatedDayNames = new String[] { "Su", "M", "Tu", "W", "Th", "F", "Sa" };
      dtfi.LongDatePattern = "ddd dd-MMM-yyyy";
      dtfi.FullDateTimePattern = originalFullDateTimePattern;
      foreach (var fmt in formats)
         Console.WriteLine("{0}: {1}", fmt, value.ToString(fmt, dtfi));
   }
}
开发者ID:.NET开发者,项目名称:System.Globalization,代码行数:30,代码来源:DateTimeFormatInfo

输出:

D: Tuesday, July 09, 2013
F: Tuesday, July 09, 2013 12:00:00 AM
f: Tuesday, July 09, 2013 12:00 AM

D: Tu 09-Jul-2013
F: Tuesday, July 09, 2013 12:00:00 AM
f: Tu 09-Jul-2013 12:00 AM

示例13: Main

//引入命名空间
using System;
using System.Globalization;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      CultureInfo enUS = CultureInfo.CreateSpecificCulture("en-US");
      DateTimeFormatInfo dtfi = enUS.DateTimeFormat;

      Console.WriteLine("Original Property Values:");
      Console.WriteLine("ShortTimePattern: " + dtfi.ShortTimePattern);
      Console.WriteLine("LongTimePattern: " + dtfi.LongTimePattern);
      Console.WriteLine("FullDateTimePattern: " + dtfi.FullDateTimePattern);
      Console.WriteLine();
      
      dtfi.LongTimePattern = ReplaceWith24HourClock(dtfi.LongTimePattern);
      dtfi.ShortTimePattern = ReplaceWith24HourClock(dtfi.ShortTimePattern);
      
      Console.WriteLine("Modififed Property Values:");
      Console.WriteLine("ShortTimePattern: " + dtfi.ShortTimePattern);
      Console.WriteLine("LongTimePattern: " + dtfi.LongTimePattern);
      Console.WriteLine("FullDateTimePattern: " + dtfi.FullDateTimePattern);
   }     
   
   private static string ReplaceWith24HourClock(string fmt)
   {
      string pattern = @"^(?<openAMPM>\s*t+\s*)? " +
                       @"(?(openAMPM) h+(?<nonHours>[^ht]+)$ " +
                       @"| \s*h+(?<nonHours>[^ht]+)\s*t+)";
      return Regex.Replace(fmt, pattern, "HH${nonHours}", 
                           RegexOptions.IgnorePatternWhitespace);   
   }
}
开发者ID:.NET开发者,项目名称:System.Globalization,代码行数:36,代码来源:DateTimeFormatInfo

输出:

Original Property Values:
ShortTimePattern: h:mm tt
LongTimePattern: h:mm:ss tt
FullDateTimePattern: dddd, MMMM dd, yyyy h:mm:ss tt

Modififed Property Values:
ShortTimePattern: HH:mm
LongTimePattern: HH:mm:ss
FullDateTimePattern: dddd, MMMM dd, yyyy HH:mm:ss

示例14: Main

//引入命名空间
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      DateTime dateValue = new DateTime(2013, 5, 18, 13, 30, 0);
      String[] formats = { "D", "f", "F" };      
      
      CultureInfo enUS = CultureInfo.CreateSpecificCulture("en-US");
      DateTimeFormatInfo dtfi = enUS.DateTimeFormat;
      String originalLongDatePattern = dtfi.LongDatePattern;

      // Display the default form of three long date formats.
      foreach (var fmt in formats)
         Console.WriteLine(dateValue.ToString(fmt, dtfi));

      Console.WriteLine();
      
      // Modify the long date pattern.
      dtfi.LongDatePattern = originalLongDatePattern + " g";
      foreach (var fmt in formats)
         Console.WriteLine(dateValue.ToString(fmt, dtfi));

      Console.WriteLine();
      
      // Change A.D. to C.E. (for Common Era)
      dtfi.LongDatePattern = originalLongDatePattern + @" 'C.E.'";
      foreach (var fmt in formats)
         Console.WriteLine(dateValue.ToString(fmt, dtfi));
   }
}
开发者ID:.NET开发者,项目名称:System.Globalization,代码行数:34,代码来源:DateTimeFormatInfo

输出:

Saturday, May 18, 2013
Saturday, May 18, 2013 1:30 PM
Saturday, May 18, 2013 1:30:00 PM

Saturday, May 18, 2013 A.D.
Saturday, May 18, 2013 A.D. 1:30 PM
Saturday, May 18, 2013 A.D. 1:30:00 PM

Saturday, May 18, 2013 C.E.
Saturday, May 18, 2013 C.E. 1:30 PM
Saturday, May 18, 2013 C.E. 1:30:00 PM

示例15: Main

//引入命名空间
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string[] dateStrings = { "08/18/2014", "01/02/2015" };
      string[] cultureNames = { "en-US", "en-GB", "fr-FR", "fi-FI" };
      
      foreach (var cultureName in cultureNames) {
         CultureInfo culture = CultureInfo.CreateSpecificCulture(cultureName);
         Console.WriteLine("Parsing strings using the {0} culture.", 
                           culture.Name);
         foreach (var dateStr in dateStrings) {
            try {
               Console.WriteLine(String.Format(culture, 
                                 "   '{0}' --> {1:D}", dateStr, 
                                 DateTime.Parse(dateStr, culture)));
            }
            catch (FormatException) {
               Console.WriteLine("   Unable to parse '{0}'", dateStr);
            }
         }
      }
   }
}
开发者ID:.NET开发者,项目名称:System.Globalization,代码行数:28,代码来源:DateTimeFormatInfo

输出:

Parsing strings using the en-US culture.
'08/18/2014' --> Monday, August 18, 2014
'01/02/2015' --> Friday, January 02, 2015
Parsing strings using the en-GB culture.
Unable to parse '08/18/2014'
'01/02/2015' --> 01 February 2015
Parsing strings using the fr-FR culture.
Unable to parse '08/18/2014'
'01/02/2015' --> dimanche 1 février 2015
Parsing strings using the fi-FI culture.
Unable to parse '08/18/2014'
'01/02/2015' --> 1. helmikuuta 2015


注:本文中的System.Globalization.DateTimeFormatInfo类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。