MainPage.xaml
<Page
x:Class="UniversalAppTutorials.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UniversalAppTutorials"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<StackPanel
x:Name="stack_panel1"
Orientation="Vertical"
Background="Snow"
Padding="50"
>
<DatePicker
x:Name="DatePicker1"
DateChanged="DatePicker1_DateChanged"
Header="Select A Date"
/>
<TextBlock
x:Name="TextBlock1"
Foreground="Navy"
FontSize="21"
TextWrapping="Wrap"
Margin="20"
/>
</StackPanel>
</Page>
MainPage.xaml.cs
using System.Globalization;
using Windows.UI.Xaml.Controls;
namespace UniversalAppTutorials
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void DatePicker1_DateChanged(object sender, DatePickerValueChangedEventArgs e)
{
// Get the instance of DatePicker object
DatePicker datePicker = sender as DatePicker;
// When date change
TextBlock1.Text = "You selected:";
// Get the DatePicker selected date
TextBlock1.Text += "\n" + datePicker.Date;
// Format DatePicker selected date using ToString method.
TextBlock1.Text += "\nShort date: " + datePicker.Date.ToString("d");
TextBlock1.Text += "\nLong date: " + datePicker.Date.ToString("D");
TextBlock1.Text += "\nFull date and short time: " + datePicker.Date.ToString("f");
TextBlock1.Text += "\nFull date and long time: " + datePicker.Date.ToString("F");
TextBlock1.Text += "\nGeneral date time (short time): " + datePicker.Date.ToString("g");
TextBlock1.Text += "\nGeneral date time (long time): " + datePicker.Date.ToString("G");
TextBlock1.Text += "\nMonth pattern: " + datePicker.Date.ToString("M");
TextBlock1.Text += "\nYear pattern: " + datePicker.Date.ToString("Y");
// This line create a culture for english in us
CultureInfo usCulture = new CultureInfo("en-US");
// Create more culture
CultureInfo frCulture = new CultureInfo("fr-FR");
CultureInfo gbCulture = new CultureInfo("en-GB");
CultureInfo dkCulture = new CultureInfo("da-DK");
// String format date using string.format method
TextBlock1.Text += "\n\nDate time format in [en-US] culture: "
+ string.Format(usCulture, "{0:d}", datePicker.Date);
TextBlock1.Text += "\nDate time format in [fr-FR] culture: "
+ string.Format(frCulture, "{0:d}", datePicker.Date);
TextBlock1.Text += "\nDate time format in [en-GB] culture: "
+ string.Format(gbCulture, "{0:d}", datePicker.Date);
TextBlock1.Text += "\nDate time format in [da-DK] culture: "
+ string.Format(dkCulture, "{0:d}", datePicker.Date);
}
}
}