Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Firends,

I am not conversant with WPF so need some help about using DatePicker control of WPF.

I am getting an XML document in which one of the field is Date field as below:

<Students>
    <Student>
        <Name>John</Name>
        <DateOfBirth>1990-12-05</DateOfBirth>
    </Student>
    <Student>
        <Name>Mike</Name>
        <DateOfBirth>1980-05-09</DateOfBirth>
    </Student>
</Students>


The date always comes in the format "yyyy-MM-dd". But I need to display the date in DatePicker in format "dd/MM/yyyy". Also when user edit the date from DatePicker, I need to save the date back in the XML in the same format i.e. "yyyy-MM-dd".

Please let me know how to do that.

Regards
Aseem
Posted
Updated 21-Jun-16 18:32pm

To format the string for saving in XML:

String.Format("{0:yyyy-MM-dd}", dt);  


You should be able to override your default format by updating the culture:

CultureInfo ci = CultureInfo.CreateSpecificCulture(CultureInfo.CurrentCulture.Name); ci.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd"; 
Thread.CurrentThread.CurrentCulture = ci; 


Then whenever you output shortdate it should be the right format

dt.ToShortDateString();


Setting the culture information should allow you to get date directly from the string:

var x = DateTime.Parse("2000-12-30");
 
Share this answer
 
v2
Comments
sayka 24-Feb-14 16:00pm    
Thank You..
Cluster.APX 27-May-16 4:17am    
This is very great solution. Thank you
You should be able to override your default format by updating the culture:


CultureInfo ci = CultureInfo.CreateSpecificCulture(CultureInfo.CurrentCulture.Name);
ci.DateTimeFormat.ShortTimePattern = "HH.mm";
Thread.CurrentThread.CurrentCulture = ci;


Then whenever you output shortdate it should be the right format
 
Share this answer
 
C#
//
// Custom format in DateTimePicker
// Why do I love this language
// Looked at the code in reflector and came to this decision,
// which does not affect the thread culture
//
// Special thanks to google translate from Russia.
//




C#
public class CustomLanguage : CultureInfo
  {
      public CustomLanguage()
          : base(CultureInfo.InvariantCulture.LCID, true)
      {
          this.DateTimeFormat = new DateTimeFormatInfo()
          {
//your format
              LongDatePattern = "yyyy/MM/dd",
              ShortDatePattern = "yyyy/MM/dd"
          };

// get internal ctor and create XmlLanguage 
          var mi = typeof(XmlLanguage).GetConstructor(
              System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance,
              null,new Type[] { typeof(string) }, null);
          Language = (XmlLanguage)mi.Invoke(new[] { "" });


// set our culture with our format
          var cu_fi = Language.GetType().GetField("_specificCulture", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
          cu_fi.SetValue(Language, this);

      }

      public XmlLanguage Language { get; private set; }

  }

XML
<datepicker language="{Binding Path=Language,Source={StaticResource CustomLanguage}}" />
 
Share this answer
 
v2
The best idea is to have a student class with Name and DateOfBirth Properties. Then in your XAML, all you need to do is bind the DatePicker to the DOB property and set the StringFormat like this
XML
<DatePicker SelectedDate="{Binding Path=DateOfBirth,StringFormat='dd/MM/yyy'}" />

Then you could have a Save method in your Student class or Data Access class that saves the DOB property in the correct format.
 
Share this answer
 
Comments
jonex77 12-Sep-12 9:22am    
I tested but was not working for me.
Cluster.APX 27-May-16 4:16am    
not work for me too.
Use the XML value text to create a DateTime[^] object, which you can then use to initialise your DatePicker. When creating the XML do the same in reverse.
 
Share this answer
 

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