Getting Difference of Two Dates in Windows Store App

Introduction

Today we will create an application in a Metro style to determine the difference between two dates in hours. To implement it we will use two Textboxes for entering the dates and the conversion code will be executed in a button click event. In the code behind file MainPage.xaml we will use the DateTime class and some properties like Year, Month etc.

In the following we are including the entire code of the XAML file and the code behind file to create this mini application. 

Step 1 : First, you will create a new Metro Style Application. Let us see the description with images of how you will create it.

  • Open Visual Studio 2012
  • File -> New -> Project
  • Choose Template -> Visual C# -> Metro Style Application
  • Rename the application

img1.gif

Step 2 : In the Solution Explorer there are two files that we will primarily work with; the MainPage.xaml and MainPage.xaml.cs files.

img2.gif

Step 3 : The MainPage.xaml file is as in the following code:

Code :

<Page

    x:Class="App1.MainPage"

    IsTabStop="false"

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

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

    xmlns:local="using:App4"

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

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

    mc:Ignorable="d">

 

    <Grid Background="Lime">

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width=".333*"></ColumnDefinition>

            <ColumnDefinition Width=".053*"></ColumnDefinition>

            <ColumnDefinition Width=".333*"></ColumnDefinition>

        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>

            <RowDefinition Height="40*"></RowDefinition>

            <RowDefinition Height="2*"/>

            <RowDefinition Height="0*"/>

            <RowDefinition Height="36*"></RowDefinition>

            <RowDefinition Height="3*"/>

            <RowDefinition Height="0*"/>

            <RowDefinition Height="32*"></RowDefinition>

            <RowDefinition Height="52*"/>

            <RowDefinition Height="5*"/>

            <RowDefinition Height="4*"/>

            <RowDefinition Height="39*"></RowDefinition>

            <RowDefinition Height="555*"></RowDefinition>

 

        </Grid.RowDefinitions>

        <TextBlock Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="3"

                   Text="Count Hours Between Two Dates" FontSize="30"

                   FontWeight="ExtraBold" HorizontalAlignment="Center"

                   Margin="515,0,389,0" Grid.RowSpan="2" Width="462"></TextBlock>

        <TextBlock Text="First Date" FontSize="20" Grid.Column="0"

                   Grid.Row="3" HorizontalAlignment="Right" FontWeight="Bold"

                   Foreground="Red" Grid.ColumnSpan="2" Margin="0,0,100,0" Width="90"/>

        <TextBlock Text="Second Date" FontSize="20" Grid.Column="0"

                   Grid.Row="6" HorizontalAlignment="Right" FontWeight="Bold"

                   Foreground="Red" Width="118" Grid.RowSpan="4"></TextBlock>

        <TextBox x:Name="txt1" FontSize="20" Grid.Column="2" Grid.Row="3"

                 HorizontalAlignment="Left" FontWeight="Bold"  Width="200" Margin="0,0,0,2"/>

        <TextBox x:Name="txt2" FontSize="20" Grid.Column="2"

                 Grid.Row="6" HorizontalAlignment="Left"

                 FontWeight="Bold"  Width="200" />

        <Button Content="click" Grid.Row="7" VerticalAlignment="Bottom"

                Width="91" Background="Red" Click="Button_Click_1"

                Height="38" Grid.Column="1"/>

        <TextBlock Text="Result" FontSize="20" Grid.Column="0" Grid.Row="10"

                   HorizontalAlignment="Right" FontWeight="Bold"

                   Foreground="Red" Width="58"></TextBlock>

        <TextBox x:Name="txt3" FontSize="20" Grid.Column="2" Grid.Row="10"

                 HorizontalAlignment="Left" FontWeight="Bold"  Width="200"

                 Margin="0,0,0,1"></TextBox>

     </Grid>

</Page>

 

Step 4 : The MainPage.xaml.cs file is as in the following code:

Code :

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using Windows.Foundation;

using Windows.Foundation.Collections;

using Windows.UI.Xaml;

using Windows.UI.Xaml.Controls;

using Windows.UI.Xaml.Controls.Primitives;

using Windows.UI.Xaml.Data;

using Windows.UI.Xaml.Input;

using Windows.UI.Xaml.Media;

using Windows.UI.Xaml.Navigation;

 

namespace App1

{

    public sealed partial class MainPage : Page

    {

        List<Person> personlist = new List<Person>();

        public MainPage()

        {

            this.InitializeComponent();

          

        }

        protected override void OnNavigatedTo(NavigationEventArgs e)

        {

        }

        private void Button_Click_1(object sender, RoutedEventArgs e)

        {

            DateTime fstdat = Convert.ToDateTime(txt1.Text);

            DateTime seconddat = Convert.ToDateTime(txt2.Text);

            int result;

            int i = seconddat.Year - fstdat.Year;

            result = i * 365 * 24;

            i = seconddat.Month - fstdat.Month;

            result = result + i  * 24;

            i = seconddat.Day - fstdat.Day;

            result = result + i * 30 * 24;

            txt3.Text = result.ToString() + " Hr";

         }

    }

}

 

Step 5 : After running this code the output looks like this:

img3.gif

img4.gif