Reader Level:
ARTICLE

Formatting In Date and Time Using Windows Store Apps

Posted by Richa Garg Articles | Windows Store Apps September 21, 2012
In this article I will explain how to perform formatting in date and time using the Windows Store apps.
  • 0
  • 0
  • 3787

Introduction

Today I am going to explain how to perform formatting in date and time. This can be done using the DataTimeFormatter class which resides in the Windows.Globalization.DateTimeFormatting class. This class is very useful when we want to perform formats and parses dates and times. This class is only applied to the Windows Store application. This class has various methods and properties so that we can format the Current date, time, day of the week, year, month and much more. In this article I will explain some of these methods so that we can perform the basic formatting of date and time.

To see how it works let's use the following steps.

Step 1

First of all open the Visual Studio 2012 and click on File -> New -> Project. A window is shown; in it select the Window Store inside the Visual C# template from the left side pane and the BlankPage from the center pane. Give the name of your application as "DateTimeFormatter" and then click ok.

Step 2

Open the MainPage.xaml file and enter the following code (to perform designing):

<Page

    x:Class="DateTimeFormatter.MainPage"

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

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

    xmlns:local="using:DateTimeFormatter"

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

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

    mc:Ignorable="d">

    <Grid>

        <Grid.Background>

            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">

                <GradientStop Color="Black"/>

                <GradientStop Color="#FFF7D1D1" Offset="1"/>

            </LinearGradientBrush>

        </Grid.Background>

        <Grid.RowDefinitions>

            <RowDefinition Height="100"/>

            <RowDefinition Height="300"/>

            <RowDefinition Height="300"/>

        </Grid.RowDefinitions>

        <TextBlock Text="DateTimeFormatter Example" Height="50" Width="350" HorizontalAlignment="Center" Grid.Row="0" FontSize="25"/>

        <StackPanel Grid.Row="1" Orientation="Horizontal">

            <StackPanel.Background>

                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">

                    <GradientStop Color="Black"/>

                    <GradientStop Color="#FFF1E7E7" Offset="1"/>

                </LinearGradientBrush>

            </StackPanel.Background>

            <StackPanel Orientation="Horizontal">

                <Button Content="Display DateFormat" Height="50" Width="200" Click="Button_Click_1" Margin="50,50,0,0" Padding="10"/>

            </StackPanel>

            <StackPanel Orientation="Horizontal">

                <TextBlock x:Name="Output_date" Height="250" Width="500" HorizontalAlignment="Left" FontSize="15" Margin="50,100,0,0"/>

            </StackPanel>

        </StackPanel>

        <StackPanel Grid.Row="2" Orientation="Horizontal">

            <StackPanel.Background>

                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">

                    <GradientStop Color="Black"/>

                    <GradientStop Color="#FFC5FD9E" Offset="1"/>

                </LinearGradientBrush>

            </StackPanel.Background>

            <StackPanel Orientation="Horizontal">

                <Button Content="Display TimeFormat" Height="50" Width="200" Click="Button_Click_2"  Padding="10" Margin="50,50,0,0"/>

            </StackPanel>

            <StackPanel Orientation="Horizontal">

                <TextBlock x:Name="Output_Time" Height="250" Width="500" HorizontalAlignment="Left" FontSize="15" Margin="50,50,0,0" />

            </StackPanel>

        </StackPanel>

    </Grid>

</Page>

Step 3

Add the following namespace in the MainPage.xaml.cs file:
 

using Windows.Globalization.DateTimeFormatting;

Step 4

Create the instance of the DateTimeFormatter class.

Step 5

Now write the code for perform formatting in Date as:

StringBuilder results = new StringBuilder();

Windows.Globalization.DateTimeFormatting.DateTimeFormatter[] templateFormatters = new[]

{               

      new  Windows.Globalization.DateTimeFormatting.DateTimeFormatter("day month"),

      new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("month year"),

      new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("day month year"),

      new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("dayofweek day month year"),

      new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("dayofweek.abbreviated"),

      new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("month.abbreviated"),

      new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("year.abbreviated"),

};

DateTime dateTime = DateTime.Now;

foreach (Windows.Globalization.DateTimeFormatting.DateTimeFormatter formatter in templateFormatters)

{

      results.AppendLine(formatter.Template + ": " + formatter.Format(dateTime));

      Output_date.Text = results.ToString();

}

Step 6

Write the code to do the formatting of Time as:

StringBuilder results = new StringBuilder();

Windows.Globalization.DateTimeFormatting.DateTimeFormatter[] templateFormatters1 = new[]

{             

      new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("hour"),

      new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("hour minute"),

      new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("hour minute second"),

};

DateTime dateTime = DateTime.Now;

foreach (Windows.Globalization.DateTimeFormatting.DateTimeFormatter formatter in templateFormatters1)

{

      results.AppendLine(formatter.Template + ": " + formatter.Format(dateTime));

      Output_Time.Text = results.ToString();

}

Step 7

The full code of MainPage.xaml.cs file is:

namespace DateTimeFormatter

{

    public sealed partial class MainPage : Page

    {

        public MainPage()

        {

            this.InitializeComponent();

        }

        private void Button_Click_1(object sender, RoutedEventArgs e)

        {

            StringBuilder results = new StringBuilder();

            Windows.Globalization.DateTimeFormatting.DateTimeFormatter[] templateFormatters = new[]

            {               

                new  Windows.Globalization.DateTimeFormatting.DateTimeFormatter("day month"),

                new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("month year"),

                new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("day month year"),

                new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("dayofweek day month year"),

                new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("dayofweek.abbreviated"),

                new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("month.abbreviated"),

                new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("year.abbreviated"),

            };

            DateTime dateTime = DateTime.Now;

            foreach (Windows.Globalization.DateTimeFormatting.DateTimeFormatter formatter in templateFormatters)

            {

                results.AppendLine(formatter.Template + ": " + formatter.Format(dateTime));

                Output_date.Text = results.ToString();

            }

        }

        private void Button_Click_2(object sender, RoutedEventArgs e)

        {

            StringBuilder results = new StringBuilder();

            Windows.Globalization.DateTimeFormatting.DateTimeFormatter[] templateFormatters1 = new[]

            {             

                new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("hour"),

                new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("hour minute"),

                new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("hour minute second"),

             };

            DateTime dateTime = DateTime.Now;

            foreach (Windows.Globalization.DateTimeFormatting.DateTimeFormatter formatter in templateFormatters1)

            {

                results.AppendLine(formatter.Template + ": " + formatter.Format(dateTime));

                Output_Time.Text = results.ToString();

            }

        }

    }

}

Step 8

Now run the application. The output screen will look like:

output-of-DateTimeFormatter-In-Windows-Store-apps.jpg

When I click on the Dispaly DateFormat button it display some date formatting as:

Date-Formatting-in-Windows-Store-apps.jpg

The TimeFormat will display like:

Date-Time-Formatting-In-Windows-Store-apps.jpg

Summary

In this article I explained how to perform formatting in Date and Time using Windows Store apps.

COMMENT USING

Trending up