1

I am writing a simple code in UWP where the user can select date from CalendarDatePicker and the app will show the selected date in a TextBlock and do some functionalities. How can I do that?

I have tried to do it with ToString() method. It shows full form of the date (ex: 1/1/2019 10:27 AM +06:00)

But I want to show the date and the month only.

Here is the code that I've written:

XAML

<CalendarDatePicker Name="calDatePicker"
         DateChanged="calDatePicker_DateChanged"
         Grid.Column="1" 
         HorizontalAlignment="Stretch" 
         VerticalAlignment="Stretch" 
         Margin="5,0,0,1"/>
    <TextBlock Name="dateText"
               Style="{StaticResource sampleText}"
               Text="Preferred Time"/>

C#:

private void calDatePicker_DateChanged(CalendarDatePicker sender, CalendarDatePickerDateChangedEventArgs args)
{
        if (calDatePicker.Date < DateTime.Now)
        {
            dateText.Text = "Order must be placed between this day and next 5 days.";
        }
        else
        {
            dateText.Text = calDatePicker.Date.ToString();
            counter = true;
        }            
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mahmudul Hasan
  • 798
  • 11
  • 35
  • This is a little problematic functionality. You can use `ToLongDateString()` or `ToShortDateString()` to display only a full date. But when you want do display only year and month, you need to use your specific format in `ToString` function. But there is problem with international compatibility. There is no way to get format for month and day only writing from system or support classes. It can be `MM/dd` *(e.g. USA)*, or `dd. MM.` *(e.g. many European countries)* or `MM月dd日` *(Japan, and perhaps China)*. And therefore you need to determine custom format, or remove year from country format. – Julo Jan 01 '19 at 04:51
  • Possible duplicate of [C# DateTime to "YYYYMMDDHHMMSS" format](https://stackoverflow.com/questions/3025361/c-sharp-datetime-to-yyyymmddhhmmss-format) – M.Armoun Jan 01 '19 at 04:56

1 Answers1

1
private void calDatePicker_DateChanged(CalendarDatePicker sender, CalendarDatePickerDateChangedEventArgs args)
{
    if (calDatePicker.Date < DateTime.Now)
    {
        dateText.Text = "Order must be placed between this day and next 5 days.";
    }
    else
    {
        var dt = calDatePicker.Date;
        dateText.Text = dt.Value.Year.ToString() + " " + dt.Value.Month.ToString();
        counter = true;
    }            
}
CodeMan
  • 671
  • 6
  • 13