wpf C# 如何从 calendar.SelectedDate 以 dd/MM/yyyy 格式获取日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20886603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:22:22  来源:igfitidea点击:

C# How to get date from calendar.SelectedDate in format dd/MM/yyyy

c#wpfcalendardatetime-format

提问by FrankieCC

I need to get selected date from calendar and show it in textbox in format dd/MM/yyyy. I have this code, but it does not work.

我需要从日历中获取选定的日期,并以 dd/MM/yyyy 格式显示在文本框中。我有这个代码,但它不起作用。

datum_odevzdani.Text = (calendar_odevzdani.SelectedDate.ToString("dd/MM/yyyy"));

回答by Clemens

The type of the Calendar.SelectedDateproperty is Nullable<DateTime>. Hence you have to access the contained DateTimeby the Nullable<T>.Valueproperty:

Calendar.SelectedDate属性的类型是Nullable<DateTime>。因此,您必须访问DateTimeNullable<T>.Value属性所包含的内容:

if (calendar_odevzdani.SelectedDate.HasValue)
{
    datum_odevzdani.Text = calendar_odevzdani.SelectedDate.Value.ToString("dd/MM/yyyy");
}