Click here to Skip to main content
12,999,157 members (42,908 online)
Rate this:
Please Sign up or sign in to vote.
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?


string date1=datePicker1.DisplayDate.ToString("yyyy-MM-dd");
Posted 24-Apr-13 17:05pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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:

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

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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170622.1 | Last Updated 25 Apr 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100