Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

DateTimeFormatInfo.GetEra Method

Returns the integer representing the specified era name.

Namespace:  System.Globalization
Assembly:  mscorlib (in mscorlib.dll)

public int GetEra(
	string eraName
)

Parameters

eraName
Type: System.String
The string containing the name of the era.

Return Value

Type: System.Int32
The integer representing the era, if eraName is valid; otherwise, -1.

ExceptionCondition
ArgumentNullException

eraName is null.

The era name is the name a calendar uses to refer to a period of time reckoned from a fixed point or event. For example, "A.D." or "C.E." is the current era in the Gregorian calendar.

The comparison with eraName is case-insensitive, for example, "A.D." is equivalent to "a.d.".

GetEra ignores punctuation in abbreviated era names, only if the GregorianCalendar is selected in DateTimeFormatInfo.Calendar and the culture uses "A.D." as the era name, that is, "A.D." is equivalent to "AD".

GetEra compares eraName with the full era name returned by GetEraName and with the abbreviated era name returned by GetAbbreviatedEraName.

Platform Notes

Silverlight for Windows Phone Silverlight for Windows Phone

 This member returns -1 for the fr-FR culture.

The following example shows that DateTimeFormatInfo ignores the punctuation in the era name, only if the calendar is Gregorian and the culture uses the era name "A.D.".


using System;
using System.Globalization;
using System.Windows.Media;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // Create an array containing selected culture names.
      string[] cultureNames = { "ar-DZ", "ar-EG", "ar-IQ", "ar-JO", "ar-LB", 
                                "ar-LY", "ar-MA", "ar-SA", "ar-SY", "ar-TN", 
                                "be-BY", "bg-BG", "cs-CZ", "da-DK", "de-AT",  
                                "de-CH", "de-DE", "dv-MV", "el-GR", "en-AU", 
                                "en-CA", "en-GB", "en-NZ", "en-US", "es-AR", 
                                "es-BO", "es-CL", "es-CO", "es-EC", "es-ES", 
                                "es-GT", "es-HN", "es-MX", "es-NI", "es-PE", 
                                "es-PY", "es-VE", "fa-IR", "fi-FI", "fr-BE",
                                "fr-FR", "he-IL", "hi-IN", "hr-HR", "hu-HU", 
                                "id-ID", "is-IS", "it-IT", "ja-JP", "ka-GE",
                                "ko-KR", "ms-MY", "nl-NL", "nn-NO", "pl-PL",  
                                "pt-BR", "pt-PT", "ro-RO", "ru-RU", "sa-IN", 
                                "sk-SK", "sl-SI", "sq-AL", "sv-SE", "sw-KE",  
                                "syr-SY", "ta-IN", "th-TH", "tr-TR", "uk-UA", 
                                "ur-PK", "vi-VN", "zh-CN", "zh-HK", "zh-SG", 
                                "zh-TW"};

       // Create strings with punctuation and without.
      String strADPunc = "A.D.";
      String strADNoPunc = "AD";
      String strCEPunc = "C.E.";
      String strCENoPunc = "CE";

      outputBlock.FontFamily = new FontFamily("Courier New");
      // Call DTFI.GetEra for each culture that uses GregorianCalendar as the default calendar.
      outputBlock.Text += "CULTURE  A.D.   AD  C.E.    CE    CALENDAR                ";
      outputBlock.Text += "CULTURE  A.D.   AD   C.E.   CE    CALENDAR \n\n";
      int ctr = 0;
      foreach (string cultureName in cultureNames)
      {
         CultureInfo culture = new CultureInfo(cultureName);
         string calendarName = culture.Calendar.ToString();
         calendarName = calendarName.Substring(calendarName.LastIndexOf('.') + 1);
         outputBlock.Text += String.Format("{0,-10}", culture);
         outputBlock.Text += String.Format("{0,-6}{1,-6}", culture.DateTimeFormat.GetEra(strADPunc), culture.DateTimeFormat.GetEra(strADNoPunc));
         outputBlock.Text += String.Format("{0,-6}{1,-6}", culture.DateTimeFormat.GetEra(strCEPunc), culture.DateTimeFormat.GetEra(strCENoPunc));
         outputBlock.Text += String.Format("{0,-20}    ", calendarName);

         ctr++;
         if (ctr == 2)
         {
            outputBlock.Text += "\n";
            ctr = 0;
         }
      }
   }

}

/*
This example produces the following output:

   CULTURE  A.D.   AD  C.E.    CE    CALENDAR                CULTURE  A.D.   AD   C.E.   CE    CALENDAR

   ar-DZ     1     1     -1    -1    GregorianCalendar       ar-EG     1     1     -1    -1    GregorianCalendar       
   ar-IQ     1     1     -1    -1    GregorianCalendar       ar-JO     1     1     -1    -1    GregorianCalendar       
   ar-LB     1     1     -1    -1    GregorianCalendar       ar-LY     1     1     -1    -1    GregorianCalendar       
   ar-MA     1     1     -1    -1    GregorianCalendar       ar-SA     -1    -1    -1    -1    HijriCalendar           
   ar-SY     1     1     -1    -1    GregorianCalendar       ar-TN     1     1     -1    -1    GregorianCalendar       
   be-BY     1     1     -1    -1    GregorianCalendar       bg-BG     1     1     -1    -1    GregorianCalendar       
   cs-CZ     -1    -1    -1    -1    GregorianCalendar       da-DK     1     1     -1    -1    GregorianCalendar       
   de-AT     -1    -1    -1    -1    GregorianCalendar       de-CH     -1    -1    -1    -1    GregorianCalendar       
   de-DE     -1    -1    -1    -1    GregorianCalendar       dv-MV     -1    -1    -1    -1    HijriCalendar           
   el-GR     -1    -1    -1    -1    GregorianCalendar       en-AU     1     1     -1    -1    GregorianCalendar       
   en-CA     1     1     -1    -1    GregorianCalendar       en-GB     1     1     -1    -1    GregorianCalendar       
   en-NZ     1     1     -1    -1    GregorianCalendar       en-US     1     1     -1    -1    GregorianCalendar       
   es-AR     -1    -1    -1    -1    GregorianCalendar       es-BO     -1    -1    -1    -1    GregorianCalendar       
   es-CL     -1    -1    -1    -1    GregorianCalendar       es-CO     -1    -1    -1    -1    GregorianCalendar       
   es-EC     -1    -1    -1    -1    GregorianCalendar       es-ES     -1    -1    -1    -1    GregorianCalendar       
   es-GT     -1    -1    -1    -1    GregorianCalendar       es-HN     -1    -1    -1    -1    GregorianCalendar       
   es-MX     -1    -1    -1    -1    GregorianCalendar       es-NI     -1    -1    -1    -1    GregorianCalendar       
   es-PE     -1    -1    -1    -1    GregorianCalendar       es-PY     -1    -1    -1    -1    GregorianCalendar       
   es-VE     -1    -1    -1    -1    GregorianCalendar       fa-IR     1     1     -1    -1    GregorianCalendar       
   fi-FI     1     1     -1    -1    GregorianCalendar       fr-BE     -1    -1    -1    -1    GregorianCalendar       
   fr-FR     -1    -1    -1    -1    GregorianCalendar       he-IL     -1    -1    -1    -1    GregorianCalendar       
   hi-IN     1     1     -1    -1    GregorianCalendar       hr-HR     1     1     -1    -1    GregorianCalendar       
   hu-HU     -1    -1    -1    -1    GregorianCalendar       id-ID     1     1     -1    -1    GregorianCalendar       
   is-IS     1     1     -1    -1    GregorianCalendar       it-IT     -1    -1    -1    -1    GregorianCalendar       
   ja-JP     -1    -1    -1    -1    GregorianCalendar       ka-GE     1     1     -1    -1    GregorianCalendar       
   ko-KR     -1    -1    -1    -1    GregorianCalendar       ms-MY     1     1     -1    -1    GregorianCalendar       
   nl-NL     1     1     -1    -1    GregorianCalendar       nn-NO     1     1     -1    -1    GregorianCalendar       
   pl-PL     1     1     -1    -1    GregorianCalendar       pt-BR     -1    -1    -1    -1    GregorianCalendar       
   pt-PT     -1    -1    -1    -1    GregorianCalendar       ro-RO     1     1     -1    -1    GregorianCalendar       
   ru-RU     1     1     -1    -1    GregorianCalendar       sa-IN     1     1     -1    -1    GregorianCalendar       
   sk-SK     -1    -1    -1    -1    GregorianCalendar       sl-SI     1     1     -1    -1    GregorianCalendar       
   sq-AL     1     1     -1    -1    GregorianCalendar       sv-SE     1     1     -1    -1    GregorianCalendar       
   sw-KE     1     1     -1    -1    GregorianCalendar       syr-SY    1     1     -1    -1    GregorianCalendar       
   ta-IN     1     1     -1    -1    GregorianCalendar       th-TH     -1    -1    -1    -1    ThaiBuddhistCalendar    
   tr-TR     1     1     -1    -1    GregorianCalendar       uk-UA     1     1     -1    -1    GregorianCalendar       
   ur-PK     1     1     -1    -1    GregorianCalendar       vi-VN     1     1     -1    -1    GregorianCalendar       
   zh-CN     -1    -1    -1    -1    GregorianCalendar       zh-HK     1     1     -1    -1    GregorianCalendar       
   zh-SG     1     1     -1    -1    GregorianCalendar       zh-TW     -1    -1    -1    -1    GregorianCalendar       
*/


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show:
© 2015 Microsoft