简体   繁体   中英

Why does WPF calendar return a time when there is no time picker?

WHy does the value of calendar.SelectedDate include a time (eg.: 22-04-2022 00:00:00) when the user has picked a date? In fact, I am interested in picking both a date and time but apparently, from what I've read, it seems like you can't pick a time from the calendar element in the program UI and I don't understand why when it also returns time in the value of Selected.Date

Is there an option to extend the calendar element to also include a timer-picker that isn't too complicated?

Code that that that returns returns dd-mm-yyyy 00:00:00

        {
            post.date = calendar.SelectedDate.ToString();
            DateLabel.Content = $"Date and time for post: {post.date}";
        }

WPF calendar has the default DateTime property. Neither only Date nor only Time it has Nullable<DateTime> default property, so it always along with Time. You would avoid it by setting below code

post.date = calendar.SelectedDate.Value.Date.ToShortDateString();

DateTime.toString() support formatting the date and time and this can be use to get only date not time as shown in the below code.

   post.date = calendar.sele.ToString("dd-MMM-yyyy");
   DateLabel.Content = $"Date and time for post: {post.date}";

WHy does the value of calendar.SelectedDate include a time (eg.: 22-04-2022 00:00:00) when the user has picked a date?

Simply because there is no date-only data type available in .NET and the Calendar control uses the DateTime type to store the selected date. That's why.

it seems like you can't pick a time from the calendar element in the program UI

Correct.

Is there an option to extend the calendar element to also include a timer-picker that isn't too complicated?

No.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM