如何仅将日期和月份从 CalendarDatePicker 转换为字符串
How to convert only date and month to string from CalendarDatePicker
我正在 UWP 中编写一个简单的代码,用户可以在其中从 CalendarDatePicker select 日期,应用程序将在 TextBlock 中显示 selected 日期并执行一些功能。我该怎么做?
我试过用 ToString() 方法来完成。它显示日期的完整形式(例如:1/1/2019 10:27 AM +06:00)
但我只想显示日期和月份。
这是我编写的代码:
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;
}
}
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;
}
}
我正在 UWP 中编写一个简单的代码,用户可以在其中从 CalendarDatePicker select 日期,应用程序将在 TextBlock 中显示 selected 日期并执行一些功能。我该怎么做?
我试过用 ToString() 方法来完成。它显示日期的完整形式(例如:1/1/2019 10:27 AM +06:00)
但我只想显示日期和月份。
这是我编写的代码:
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;
}
}
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;
}
}