WPF Datepicker - SelectedDate only date, not time

I know it might be a duplicate, but using the .NET framework, I cannot get the date and not the time for WPF DatePicker

I have it

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 this http://msdn.microsoft.com/en-us/library/system.datetime.date.aspx so I'm not sure what I am doing wrong.

I'm sure this is something silly, as always. But without telling me anything useful, I thought I was resorting to SO!

thank

+3


source to share


5 answers


DatePicker.SelectedDate.Value.Date

- property DateTime

Nullable. Thus, any property DateTime

has a date and time. but in this case it is not populated with the correct value and it will give you the default value. if you want the date to only use the following code,



DatePicker.SelectedDate.Value.Date.ToShortDateString()

      

+12


source


SelectedDate

the property is of type Nullable<DateTime>

, it gives you the date, however the time is reset as12:00:00 midnight (00:00:00)

see DatePicker.SelectedDate property for details



from MSDN: DateTime.Date Property

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

+2


source


The following example uses the Date property to retrieve the date component of a DateTime value when its time component is set to zero (or 0:00:00 or midnight). It also illustrates that depending on the format string used to display the DateTime value, the time component may continue to be displayed in the formatted output.

This suggests that depending on the format (in this case DatePicker, I think) a Time component may appear. As in your link, the exit is 12:00 AM

So check the format / properties of the DatePicker.

+1


source


When selecting a date from a datepicker in your code, you can do the following:

    DateTime from = From_Date_Picker.SelectedDate.Value.Date

      

+1


source


Make sure Windows regional settings are set to 24 hour clock. I guess its return 12AM

0


source







All Articles