Retrieve Current Date Time Information in Windows Store Apps

In today's article we are going to learn how to reterieve calendar statistics using the Calendar class in Windows Store apps. The Calendar class enables the developer to dispaly date and time related information in various cultures such English, Japanese etc. You can also do this using JavaScript.

The Calendar class has many built-in functions that can be used to dispaly calendar information such as Day of Month, Day of Week, Year etc. The Calendar class resides in the Windows.Globalization namespace.

In this article I will create an application that shows you the statistics for the current calendar date and time according to the various cultures or countries.

Here are the instructions for displaying current calendar information depending on culture.

Step 1

Create a Blank Windows Store App using XAML\C#.

Step 2

Include the namespace to access the Calendar Class as:

Using Windows.Globalization;

Step 3

Here is the code of the XAML file to dispaly the information:

<Page

    x:Class="CalenderSample.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="using:CalenderSample"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d">

 

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">

        <StackPanel Orientation="Vertical" VerticalAlignment="Top" Margin="30,50">

            <ListView x:Name="Calender" Background="Green" Height="400" Width="386" SelectionChanged="ListView_SelectionChanged_1" >

                <ListViewItem Height="100">

                    <StackPanel Orientation="Vertical">

                        <TextBlock Text="Hindi Calendar" FontSize="40"></TextBlock>

                    </StackPanel>

                </ListViewItem>

                <ListViewItem Height="100">

                    <StackPanel Orientation="Vertical">

                        <TextBlock Text="English Calendar" FontSize="40"></TextBlock>

                    </StackPanel>

                </ListViewItem>

                <ListViewItem Height="100">

                    <StackPanel Orientation="Vertical">

                        <TextBlock Text="Chinese Calendar" FontSize="40"></TextBlock>

                    </StackPanel>

                </ListViewItem>

                <ListViewItem Height="100">

                    <StackPanel Orientation="Vertical">

                        <TextBlock Text="French Calendar" FontSize="40"></TextBlock>

                    </StackPanel>

                </ListViewItem>

            </ListView>

            <Grid HorizontalAlignment="Center" Margin="50" Height="500">

                <Grid.RowDefinitions>

                    <RowDefinition></RowDefinition>

                    <RowDefinition></RowDefinition>

                    <RowDefinition></RowDefinition>

                    <RowDefinition></RowDefinition>

                    <RowDefinition></RowDefinition>

                </Grid.RowDefinitions>

                <Grid.ColumnDefinitions>

                    <ColumnDefinition Width="auto"></ColumnDefinition>

                </Grid.ColumnDefinitions>

                <TextBlock x:Name="dayofmonth" Grid.Row="0" Grid.Column="0"  FontSize="40"  ></TextBlock>

                <TextBlock x:Name="numberofmonth" Grid.Row="1" Grid.Column="0"  FontSize="40" ></TextBlock>

                <TextBlock x:Name="daysofweek" Grid.Row="2" Grid.Column="0" FontSize="40" ></TextBlock>

                <TextBlock x:Name="year" Grid.Row="3" Grid.Column="0" FontSize="40" ></TextBlock>

                <TextBlock x:Name="numberofdays" Grid.Row="4" Grid.Column="0" FontSize="40" ></TextBlock>

            </Grid>

        </StackPanel>

    </Grid>

</Page>


Step 4

In this step we use the Calendar class to fetch the current date and time statictics according to the culture; see:

Calendar calendar = new Calendar(new[] { "en-us" });

numberofmonth.Text = "Name of Month: " + calendar.MonthAsSoloString();

dayofmonth.Text = "Day of Month: " + calendar.DayAsPaddedString(2);

daysofweek.Text = "Day of Week: " + calendar.DayOfWeekAsSoloString();

year.Text = "Year: " + calendar.YearAsString();

numberofdays.Text = "Days in this Month: " + calendar.NumberOfDaysInThisMonth.ToString();

In the preceding code I define the code of the culture in the Constructor of the Calendar class. Then, we can use various methods of this class to fetch various information.

Here you will see the output

Globlization-in-Windows-Store-apps.jpg

Select any option from the given list.

Calendar-In-Windows-Store-Apps.jpg


Similar Articles