Share via


DateTimeFormatInfo.GetEra Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Returns the integer representing the specified era name.

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

Syntax

'Declaration
Public Function GetEra ( _
    eraName As String _
) As Integer
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.

Exceptions

Exception Condition
ArgumentNullException

eraName is nulla null reference (Nothing in Visual Basic).

Remarks

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.

Examples

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.".

Imports System.Globalization
Imports System.Windows.Media

Public Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      ' Create an array containing selected culture names.
      Dim cultureNames() As String = { "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.
      Dim strADPunc As String = "A.D."
      Dim strADNoPunc As String = "AD"
      Dim strCEPunc As String = "C.E."
      Dim strCENoPunc As String = "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" + vbCrLf + vbCrLf
      Dim ctr As Integer = 0
      For Each cultureName As String In cultureNames
         Dim culture As New CultureInfo(cultureName)
         Dim calendarName As String = culture.Calendar.ToString()
         calendarName = calendarName.Substring(calendarName.LastINdexOf("."c) + 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 += 1
         If ctr = 2 Then
            outputBlock.Text &= vbCrLf
            ctr = 0
         End If
      Next 
   End Sub  
End Class
' 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       
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       
*/

Version Information

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

Platforms

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