Click here to Skip to main content
16,015,531 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone.

I am trying to format date in datepicker. I am using the DisplayDate and it is formatting the date correctly but it is taking in the current date and not the date selected in datepicker. What am I doing wrong?


Code

C#
string date1=datePicker1.DisplayDate.ToString("yyyy-MM-dd");
Posted

1 solution

The Display date by default is the current date (DateTime.Now) and can be specified as an attribute on the date picker control in your xaml or within code.

If you are trying to get the date and format you will need to do something like the following:

HTML
<DatePicker HorizontalAlignment="Left" Margin="109,115,0,0" VerticalAlignment="Top" Name="DatePicker1"/>


C#
string date1 = Convert.ToDateTime(DatePicker1.Text).ToString("yyyy-MM-dd");


Of course you could turn this into an extension method or add some safe guards so it won't blow up if you dont select a date at all. But that will allow you to format your selected date as you desire.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900