Click here to Skip to main content
Rate this: bad
good
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?
 

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

1 solution

Rate this: bad
good
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.
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 587
1 Sergey Alexandrovich Kryukov 479
2 Maciej Los 305
3 Mathew Soji 195
4 Afzaal Ahmad Zeeshan 170
0 OriginalGriff 7,356
1 Sergey Alexandrovich Kryukov 6,777
2 DamithSL 5,461
3 Manas Bhardwaj 4,916
4 Maciej Los 4,475


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 25 Apr 2013
Copyright © CodeProject, 1999-2014
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