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

Time:04-28

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}";
        }

CodePudding user response:

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();

CodePudding user response:

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}";
  • Related