diff --git a/docs/standard/datetime/snippets/working-with-calendars/csharp/ChangeCalendar.csproj b/docs/standard/datetime/snippets/working-with-calendars/csharp/ChangeCalendar.csproj new file mode 100644 index 0000000000000..ed9781c223ab9 --- /dev/null +++ b/docs/standard/datetime/snippets/working-with-calendars/csharp/ChangeCalendar.csproj @@ -0,0 +1,10 @@ + + + + Exe + net10.0 + enable + enable + + + diff --git a/docs/standard/datetime/snippets/working-with-calendars/csharp/Program.cs b/docs/standard/datetime/snippets/working-with-calendars/csharp/Program.cs new file mode 100644 index 0000000000000..931e92115fd7e --- /dev/null +++ b/docs/standard/datetime/snippets/working-with-calendars/csharp/Program.cs @@ -0,0 +1,51 @@ +// +using System.Globalization; + +DateTime date1 = new(2011, 6, 20); + +Console.OutputEncoding = System.Text.Encoding.UTF8; +DisplayCurrentInfo(); +// Display the date using the current culture and calendar. +Console.WriteLine(date1.ToString("d")); +Console.WriteLine(); + +CultureInfo arSA = CultureInfo.CreateSpecificCulture("ar-SA"); + +// Change the current culture to Arabic (Saudi Arabia). +CultureInfo.CurrentCulture = arSA; +// Display date and information about the current culture. +DisplayCurrentInfo(); +Console.WriteLine(date1.ToString("d")); +Console.WriteLine(); + +// Change the calendar to Hijri. +Calendar hijri = new HijriCalendar(); +if (CalendarExists(arSA, hijri)) +{ + arSA.DateTimeFormat.Calendar = hijri; + // Display date and information about the current culture. + DisplayCurrentInfo(); + Console.WriteLine(date1.ToString("d")); +} + +static void DisplayCurrentInfo() +{ + Console.WriteLine($"Current Culture: {CultureInfo.CurrentCulture.Name}"); + Console.WriteLine($"Current Calendar: {DateTimeFormatInfo.CurrentInfo.Calendar}"); +} + +static bool CalendarExists(CultureInfo culture, Calendar cal) => + culture.OptionalCalendars.Any(optional => optional.ToString() == cal.ToString()); +// The example displays the following output: +// Current Culture: en-US +// Current Calendar: System.Globalization.GregorianCalendar +// 6/20/2011 +// +// Current Culture: ar-SA +// Current Calendar: System.Globalization.UmAlQuraCalendar +// 18‏‏/7‏‏/1432 بعد الهجرة +// +// Current Culture: ar-SA +// Current Calendar: System.Globalization.HijriCalendar +// 19‏‏/7‏‏/1432 بعد الهجرة +// diff --git a/docs/standard/datetime/snippets/working-with-calendars/vb/ChangeCalendar.vbproj b/docs/standard/datetime/snippets/working-with-calendars/vb/ChangeCalendar.vbproj new file mode 100644 index 0000000000000..7244b18a8a7c9 --- /dev/null +++ b/docs/standard/datetime/snippets/working-with-calendars/vb/ChangeCalendar.vbproj @@ -0,0 +1,9 @@ + + + + Exe + ChangeCalendar + net10.0 + + + diff --git a/samples/snippets/visualbasic/VS_Snippets_CLR/conceptual.calendars/vb/changecalendar2.vb b/docs/standard/datetime/snippets/working-with-calendars/vb/Program.vb similarity index 65% rename from samples/snippets/visualbasic/VS_Snippets_CLR/conceptual.calendars/vb/changecalendar2.vb rename to docs/standard/datetime/snippets/working-with-calendars/vb/Program.vb index 3de919e3ad1ab..20fe05507d683 100644 --- a/samples/snippets/visualbasic/VS_Snippets_CLR/conceptual.calendars/vb/changecalendar2.vb +++ b/docs/standard/datetime/snippets/working-with-calendars/vb/Program.vb @@ -1,14 +1,11 @@ -' Visual Basic .NET Document -Option Strict On - -' +' Imports System.Globalization -Imports System.Threading Module Example Public Sub Main() Dim date1 As Date = #6/20/2011# + Console.OutputEncoding = System.Text.Encoding.UTF8 DisplayCurrentInfo() ' Display the date using the current culture and calendar. Console.WriteLine(date1.ToString("d")) @@ -17,7 +14,7 @@ Module Example Dim arSA As CultureInfo = CultureInfo.CreateSpecificCulture("ar-SA") ' Change the current culture to Arabic (Saudi Arabia). - Thread.CurrentThread.CurrentCulture = arSA + CultureInfo.CurrentCulture = arSA ' Display date and information about the current culture. DisplayCurrentInfo() Console.WriteLine(date1.ToString("d")) @@ -34,18 +31,12 @@ Module Example End Sub Private Sub DisplayCurrentInfo() - Console.WriteLine("Current Culture: {0}", - CultureInfo.CurrentCulture.Name) - Console.WriteLine("Current Calendar: {0}", - DateTimeFormatInfo.CurrentInfo.Calendar) + Console.WriteLine($"Current Culture: {CultureInfo.CurrentCulture.Name}") + Console.WriteLine($"Current Calendar: {DateTimeFormatInfo.CurrentInfo.Calendar}") End Sub - Private Function CalendarExists(ByVal culture As CultureInfo, - cal As Calendar) As Boolean - For Each optionalCalendar As Calendar In culture.OptionalCalendars - If cal.ToString().Equals(optionalCalendar.ToString()) Then Return True - Next - Return False + Private Function CalendarExists(culture As CultureInfo, cal As Calendar) As Boolean + Return culture.OptionalCalendars.Any(Function(optional1) optional1.ToString() = cal.ToString()) End Function End Module ' The example displays the following output: @@ -55,9 +46,9 @@ End Module ' ' Current Culture: ar-SA ' Current Calendar: System.Globalization.UmAlQuraCalendar -' 18/07/32 +' 18‏‏/7‏‏/1432 بعد الهجرة ' ' Current Culture: ar-SA ' Current Calendar: System.Globalization.HijriCalendar -' 19/07/32 -' +' 19‏‏/7‏‏/1432 بعد الهجرة +' diff --git a/docs/standard/datetime/working-with-calendars.md b/docs/standard/datetime/working-with-calendars.md index 6b6c6767852d2..f2d1928571114 100644 --- a/docs/standard/datetime/working-with-calendars.md +++ b/docs/standard/datetime/working-with-calendars.md @@ -71,8 +71,10 @@ The calendar currently in use by a particular value and displays it using the current culture - which, in this case, is English (United States) - and the current culture's calendar (which, in this case, is the Gregorian calendar). Next, it changes the current culture to Arabic (Saudi Arabia) and displays the date using its default Um Al-Qura calendar. It then calls the `CalendarExists` method to determine whether the Hijri calendar is supported by the Arabic (Saudi Arabia) culture. Because the calendar is supported, it changes the current calendar to Hijri and again displays the date. Note that in each case, the date is displayed using the current culture's current calendar. -[!code-csharp[Conceptual.Calendars#2](../../../samples/snippets/csharp/VS_Snippets_CLR/conceptual.calendars/cs/changecalendar2.cs#2)] -[!code-vb[Conceptual.Calendars#2](../../../samples/snippets/visualbasic/VS_Snippets_CLR/conceptual.calendars/vb/changecalendar2.vb#2)] +:::code language="csharp" source="./snippets/working-with-calendars/csharp/Program.cs" id="ChangeCalendar"::: +:::code language="vb" source="./snippets/working-with-calendars/vb/Program.vb" id="ChangeCalendar"::: + +Before the example writes to the console, it changes the console's output encoding to UTF-8 so that Arabic letters display correctly. ## Dates and calendars diff --git a/samples/snippets/csharp/VS_Snippets_CLR/conceptual.calendars/cs/changecalendar2.cs b/samples/snippets/csharp/VS_Snippets_CLR/conceptual.calendars/cs/changecalendar2.cs deleted file mode 100644 index 87a600d442d67..0000000000000 --- a/samples/snippets/csharp/VS_Snippets_CLR/conceptual.calendars/cs/changecalendar2.cs +++ /dev/null @@ -1,63 +0,0 @@ -// -using System; -using System.Globalization; -using System.Threading; - -public class Example -{ - public static void Main() - { - DateTime date1 = new DateTime(2011, 6, 20); - - DisplayCurrentInfo(); - // Display the date using the current culture and calendar. - Console.WriteLine(date1.ToString("d")); - Console.WriteLine(); - - CultureInfo arSA = CultureInfo.CreateSpecificCulture("ar-SA"); - - // Change the current culture to Arabic (Saudi Arabia). - Thread.CurrentThread.CurrentCulture = arSA; - // Display date and information about the current culture. - DisplayCurrentInfo(); - Console.WriteLine(date1.ToString("d")); - Console.WriteLine(); - - // Change the calendar to Hijri. - Calendar hijri = new HijriCalendar(); - if (CalendarExists(arSA, hijri)) { - arSA.DateTimeFormat.Calendar = hijri; - // Display date and information about the current culture. - DisplayCurrentInfo(); - Console.WriteLine(date1.ToString("d")); - } - } - - private static void DisplayCurrentInfo() - { - Console.WriteLine($"Current Culture: {CultureInfo.CurrentCulture.Name}"); - Console.WriteLine($"Current Calendar: {DateTimeFormatInfo.CurrentInfo.Calendar}"); - } - - private static bool CalendarExists(CultureInfo culture, Calendar cal) - { - foreach (Calendar optionalCalendar in culture.OptionalCalendars) - if (cal.ToString().Equals(optionalCalendar.ToString())) - return true; - - return false; - } -} -// The example displays the following output: -// Current Culture: en-US -// Current Calendar: System.Globalization.GregorianCalendar -// 6/20/2011 -// -// Current Culture: ar-SA -// Current Calendar: System.Globalization.UmAlQuraCalendar -// 18/07/32 -// -// Current Culture: ar-SA -// Current Calendar: System.Globalization.HijriCalendar -// 19/07/32 -//