แก้ไข

แชร์ผ่าน


DateTimeFormatInfo.GetEra(String) Method

Definition

Returns the integer representing the specified era.

public:
 int GetEra(System::String ^ eraName);
public int GetEra (string eraName);
member this.GetEra : string -> int
Public Function GetEra (eraName As String) As Integer

Parameters

eraName
String

The string containing the name of the era.

Returns

The integer representing the era, if eraName is valid; otherwise, -1.

Exceptions

eraName is null.

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

using namespace System;
using namespace System::Globalization;
using namespace System::Collections;
int main()
{
   
   // Creates strings with punctuation and without.
   String^ strADPunc = "A.D.";
   String^ strADNoPunc = "AD";
   String^ strCEPunc = "C.E.";
   String^ strCENoPunc = "CE";
   
   // Calls DTFI::GetEra for each culture that uses GregorianCalendar as the default calendar.
   Console::WriteLine( "            ----- AD -----  ----- CE -----" );
   Console::WriteLine( "CULTURE     PUNC   NO PUNC  PUNC   NO PUNC  CALENDAR" );
   IEnumerator^ en = CultureInfo::GetCultures( CultureTypes::SpecificCultures )->GetEnumerator();
   while ( en->MoveNext() )
   {
      CultureInfo^ myCI = safe_cast<CultureInfo^>(en->Current);
      Console::Write( "{0, -12}", myCI );
      Console::Write( "{0,-7}{1,-9}", myCI->DateTimeFormat->GetEra( strADPunc ), myCI->DateTimeFormat->GetEra( strADNoPunc ) );
      Console::Write( "{0, -7}{1, -9}", myCI->DateTimeFormat->GetEra( strCEPunc ), myCI->DateTimeFormat->GetEra( strCENoPunc ) );
      Console::Write( "{0}", myCI->Calendar );
      Console::WriteLine();
   }
}

/*
This code produces the following output.  This output has been cropped for brevity.

            ----- AD -----  ----- CE -----
CULTURE     PUNC   NO PUNC  PUNC   NO PUNC  CALENDAR
ar-SA       -1     -1       -1     -1       System.Globalization.HijriCalendar
ar-IQ       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-EG       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-LY       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-DZ       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-MA       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-TN       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-OM       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-YE       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-SY       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-JO       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-LB       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-KW       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-AE       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-BH       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-QA       1      1        -1     -1       System.Globalization.GregorianCalendar
bg-BG       1      1        -1     -1       System.Globalization.GregorianCalendar
ca-ES       -1     -1       -1     -1       System.Globalization.GregorianCalendar
zh-TW       -1     -1       -1     -1       System.Globalization.GregorianCalendar
zh-CN       -1     -1       -1     -1       System.Globalization.GregorianCalendar
zh-HK       1      1        -1     -1       System.Globalization.GregorianCalendar
zh-SG       1      1        -1     -1       System.Globalization.GregorianCalendar
zh-MO       1      1        -1     -1       System.Globalization.GregorianCalendar
cs-CZ       -1     -1       -1     -1       System.Globalization.GregorianCalendar
da-DK       1      1        -1     -1       System.Globalization.GregorianCalendar

*/
using System;
using System.Globalization;

public class SamplesGregorianCalendar  {

   public static void Main()  {

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

      // Calls DTFI.GetEra for each culture that uses GregorianCalendar as the default calendar.
      Console.WriteLine( "            ----- AD -----  ----- CE -----" );
      Console.WriteLine( "CULTURE     PUNC   NO PUNC  PUNC   NO PUNC  CALENDAR" );
      foreach ( CultureInfo myCI in CultureInfo.GetCultures( CultureTypes.SpecificCultures ) )  {
         Console.Write( "{0,-12}", myCI );
         Console.Write( "{0,-7}{1,-9}", myCI.DateTimeFormat.GetEra( strADPunc ), myCI.DateTimeFormat.GetEra( strADNoPunc ) );
         Console.Write( "{0,-7}{1,-9}", myCI.DateTimeFormat.GetEra( strCEPunc ), myCI.DateTimeFormat.GetEra( strCENoPunc ) );
         Console.Write( "{0}", myCI.Calendar );
         Console.WriteLine();
      }
   }
}

/*
This code produces the following output.  This output has been cropped for brevity.

            ----- AD -----  ----- CE -----
CULTURE     PUNC   NO PUNC  PUNC   NO PUNC  CALENDAR
ar-SA       -1     -1       -1     -1       System.Globalization.HijriCalendar
ar-IQ       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-EG       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-LY       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-DZ       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-MA       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-TN       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-OM       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-YE       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-SY       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-JO       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-LB       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-KW       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-AE       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-BH       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-QA       1      1        -1     -1       System.Globalization.GregorianCalendar
bg-BG       1      1        -1     -1       System.Globalization.GregorianCalendar
ca-ES       -1     -1       -1     -1       System.Globalization.GregorianCalendar
zh-TW       -1     -1       -1     -1       System.Globalization.GregorianCalendar
zh-CN       -1     -1       -1     -1       System.Globalization.GregorianCalendar
zh-HK       -1     -1       -1     -1       System.Globalization.GregorianCalendar
zh-SG       1      1        -1     -1       System.Globalization.GregorianCalendar
zh-MO       1      1        -1     -1       System.Globalization.GregorianCalendar
cs-CZ       -1     -1       -1     -1       System.Globalization.GregorianCalendar
da-DK       1      1        -1     -1       System.Globalization.GregorianCalendar

*/
Imports System.Globalization

Public Class SamplesGregorianCalendar

   Public Shared Sub Main()

      ' Creates 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"

      ' Calls DTFI.GetEra for each culture that uses GregorianCalendar as the default calendar.
      Console.WriteLine("            ----- AD -----  ----- CE -----")
      Console.WriteLine("CULTURE     PUNC   NO PUNC  PUNC   NO PUNC  CALENDAR")
      Dim myCI As CultureInfo
      For Each myCI In  CultureInfo.GetCultures(CultureTypes.SpecificCultures)
         Console.Write("{0,-12}", myCI)
         Console.Write("{0,-7}{1,-9}", myCI.DateTimeFormat.GetEra(strADPunc), myCI.DateTimeFormat.GetEra(strADNoPunc))
         Console.Write("{0,-7}{1,-9}", myCI.DateTimeFormat.GetEra(strCEPunc), myCI.DateTimeFormat.GetEra(strCENoPunc))
         Console.Write("{0}", myCI.Calendar)
         Console.WriteLine()
      Next myCI

   End Sub

End Class


'This code produces the following output.  This output has been cropped for brevity.
'
'            ----- AD -----  ----- CE -----
'CULTURE     PUNC   NO PUNC  PUNC   NO PUNC  CALENDAR
'ar-SA       -1     -1       -1     -1       System.Globalization.HijriCalendar
'ar-IQ       1      1        -1     -1       System.Globalization.GregorianCalendar
'ar-EG       1      1        -1     -1       System.Globalization.GregorianCalendar
'ar-LY       1      1        -1     -1       System.Globalization.GregorianCalendar
'ar-DZ       1      1        -1     -1       System.Globalization.GregorianCalendar
'ar-MA       1      1        -1     -1       System.Globalization.GregorianCalendar
'ar-TN       1      1        -1     -1       System.Globalization.GregorianCalendar
'ar-OM       1      1        -1     -1       System.Globalization.GregorianCalendar
'ar-YE       1      1        -1     -1       System.Globalization.GregorianCalendar
'ar-SY       1      1        -1     -1       System.Globalization.GregorianCalendar
'ar-JO       1      1        -1     -1       System.Globalization.GregorianCalendar
'ar-LB       1      1        -1     -1       System.Globalization.GregorianCalendar
'ar-KW       1      1        -1     -1       System.Globalization.GregorianCalendar
'ar-AE       1      1        -1     -1       System.Globalization.GregorianCalendar
'ar-BH       1      1        -1     -1       System.Globalization.GregorianCalendar
'ar-QA       1      1        -1     -1       System.Globalization.GregorianCalendar
'bg-BG       1      1        -1     -1       System.Globalization.GregorianCalendar
'ca-ES       -1     -1       -1     -1       System.Globalization.GregorianCalendar
'zh-TW       -1     -1       -1     -1       System.Globalization.GregorianCalendar
'zh-CN       -1     -1       -1     -1       System.Globalization.GregorianCalendar
'zh-HK       -1     -1       -1     -1       System.Globalization.GregorianCalendar
'zh-SG       1      1        -1     -1       System.Globalization.GregorianCalendar
'zh-MO       1      1        -1     -1       System.Globalization.GregorianCalendar
'cs-CZ       -1     -1       -1     -1       System.Globalization.GregorianCalendar
'da-DK       1      1        -1     -1       System.Globalization.GregorianCalendar

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.

Important

Eras in the Japanese calendars are based on the emperor's reign and are therefore expected to change. For example, May 1, 2019 marked the beginning of the Reiwa era in the JapaneseCalendar and JapaneseLunisolarCalendar. Such a change of era affects all applications that use these calendars. For more information and to determine whether your applications are affected, see Handling a new era in the Japanese calendar in .NET. For information on testing your applications on Windows systems to ensure their readiness for the era change, see Prepare your application for the Japanese era change. For features in .NET that support calendars with multiple eras and for best practices when working with calendars that support multiple eras, see Working with eras.

Applies to

See also