Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// <ChangeCalendar>
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 بعد الهجرة
// </ChangeCalendar>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<RootNamespace>ChangeCalendar</RootNamespace>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
' Visual Basic .NET Document
Option Strict On

' <Snippet2>
' <ChangeCalendar>
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"))
Expand All @@ -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"))
Expand All @@ -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:
Expand All @@ -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
' </Snippet2>
' 19‏‏/7‏‏/1432 بعد الهجرة
' </ChangeCalendar>
6 changes: 4 additions & 2 deletions docs/standard/datetime/working-with-calendars.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ The calendar currently in use by a particular <xref:System.Globalization.Culture

The following example changes the calendar used by the Arabic (Saudi Arabia) culture. It first instantiates a <xref:System.DateTime> 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

Expand Down

This file was deleted.

Loading