Click here to Skip to main content
11,715,022 members (72,680 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# WPF Help
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
0 Sergey Alexandrovich Kryukov 520
1 Richard MacCutchan 275
2 Maciej Los 259
3 ManasKumarM 155
4 Frankie-C 125
0 Sergey Alexandrovich Kryukov 775
1 CHill60 415
2 Richard MacCutchan 405
3 Maciej Los 389
4 OriginalGriff 320

Advertise | Privacy | Mobile
Web03 | 2.8.150901.1 | Last Updated 25 Apr 2013
Copyright © CodeProject, 1999-2015
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