Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Datepicker - SelectedDate just date and not time

I know this may be a duplicate but using the .NET framework I can't seem to get the date and not the time for a WPF DatePicker.

I have this:

DatePicker.SelectedDate.Value.Date

I believed using the "Date" property would only return the date, but it returns the time "12:00:00" as well.

I read https://learn.microsoft.com/en-us/dotnet/api/system.datetime.date so I'm not sure what I am doing wrong.

I'm sure it's something silly as always, but with no helpful eye with me to tell me, I thought I resort to SO!

like image 984
user3428422 Avatar asked Aug 14 '14 09:08

user3428422


4 Answers

DatePicker.SelectedDate.Value.Date is a DateTime Nullable property. So any DateTime Property has the date and time section. but in this case it is not fill with the correct value and it will gives you the default value. if you want to get the date only use following code,

DatePicker.SelectedDate.Value.Date.ToShortDateString()
like image 165
Chamika Sandamal Avatar answered Oct 10 '22 06:10

Chamika Sandamal


SelectedDate property is of type Nullable<DateTime> it gives you the date however the time is reset as 12:00:00 midnight (00:00:00)

see more on DatePicker.SelectedDate Property

from MSDN: DateTime.Date Property

The value of the Kind property of the returned DateTime value is the same as that of the current instance. Because the DateTime type represents both dates and times in a single type, it is important to avoid misinterpreting a date returned by the Date property as a date and time. For more information, see "Saving and Restoring DateTime Values" in the DateTime topic.

like image 35
pushpraj Avatar answered Oct 10 '22 04:10

pushpraj


The following example uses the Date property to extract the date component of a DateTime value with its time component set to zero (or 0:00:00, or midnight). It also illustrates that, depending on the format string used when displaying the DateTime value, the time component can continue to appear in formatted output.

That says that depending on the Format (in this case the DatePicker i think) the Time component can appear. Like in your link, the Output is 12:00 AM

So Check the Format / properties of the DatePicker.

like image 1
S.L. Avatar answered Oct 10 '22 04:10

S.L.


While selecting the date from the datepicker in you code you can do following:

    DateTime from = From_Date_Picker.SelectedDate.Value.Date
like image 2
Rakesh Avatar answered Oct 10 '22 04:10

Rakesh