Skip to main content

MSDN

Answered Question Custom date format in DatePicker in Beta 2.0RSS Feed

(0)

pthakkar
pthakkar

Member

Member

0 points

4 Posts

Custom date format in DatePicker in Beta 2.0

Hi all,

I am trying to set my custom date format like dd-MMM-yyyy to datepicker control in silverlight 2 beta 2.

I have tried as following way:

datepicker.SelectedDate = DateTime.Today;

datepicker.Text =  datepicker.SelectedDate.Value.ToString("dd-MMM-yyyy");

on load event of it. But fail to get current date in formate like 13-Nov-2008.

I need help here..

 

Thanks

Pratik 

preishuber
preishuber

Contributor

Contributor

3572 points

656 Posts

Answered Question

Re: Custom date format in DatePicker in Beta 2.0

as silverlight 2 is unsuported in all beta editions i suggest you to change to RTW bits and then take the toolkit http://www.codeplex.com/Silverlight

Source code is included

-Hannes

http://www.ppedv.de

Amanda Wang - MSFT
Amanda ...

All-Star

All-Star

17237 points

1,466 Posts

Answered Question

Re: Custom date format in DatePicker in Beta 2.0

Hi Pratik ,

We have a test on our labs with the silverlight RTW.

private void picker_Loaded(object sender, RoutedEventArgs e)// the datapicker's load event.
        {
            this.picker.Text = DateTime.Now.ToString("dd-MMM-yyyy");
        }

        private void picker_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            this.picker.Text = this.picker.SelectedDate.Value.ToString("dd-MMM-yyyy");
        }

If you add the code:  this.picker.Text = this.picker.SelectedDate.Value.ToString("dd-MMM-yyyy"); to the datapicker;s SelectedDateChanged event, you can get the correct date formate in the load event.

Amanda Wang
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.

jcmoore0
jcmoore0

Member

Member

6 points

4 Posts

Re: Re: Custom date format in DatePicker in Beta 2.0

Hi Amanda,

I hope they add a custom formatter property to the DatePicker other than just (Long, Short)

I was not able to use the SelectedDateChanged for formatting because Editing causes changes before Editing is completed.   I tried LostFocus but it continuously looped during my test.   I think it may be a known issue with LostFocus.

Thanks,

John

John

virendrakr
virendrakr

Member

Member

6 points

3 Posts

Re: Re: Custom date format in DatePicker in Beta 2.0

Hi Amanda, I tried it in Silverlight 2. It did not work. My system date format is "dd-MMM-yyyy" and i am trying to format it in "MM/dd/yyyy" format. This SelectedDateChange event was falling in loop therefore i used CalenderClosed event but no luck. Thanks

Amanda Wang - MSFT
Amanda ...

All-Star

All-Star

17237 points

1,466 Posts

Re: Re: Custom date format in DatePicker in Beta 2.0

 Hi kent,

Hi,

You can try to add the namespace using System.Globalization; using System.Threading; in the app.xaml.cs file, and add below two lines code in the Application_Startup event:

  Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");
   Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = "dd-MMM-yyyy";

 for example:

 

 private void Application_Startup(object sender, StartupEventArgs e)
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");
            Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = "dd-MMM-yyyy";
            this.RootVisual = new Page();
        }

Amanda Wang
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.

David H
David H

Member

Member

9 points

21 Posts

Re: Re: Custom date format in DatePicker in Beta 2.0

Hi Amanda,

I'm running Silverlight 2.0 and my machine's regional setting date format is set to M/dd/yyyy, when I run the following code, it returns the correct format in Silverlight as I specify.

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-us");

Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = "dd-MMM-yyyy";

 However, when I change my machine's regional setting date format to M-dd-yyyy and run the following code, it does not returns the correct format as I specify.

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-us");

Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = "dd/MMM/yyyy";

Instead, it returns 09-Mar-2009 and looks like it still use the Date Separator from regional settings.  Did I do anything wrong here?  Or is it a bug in Silverlight 2.0?

Thanks

David

David H
David H

Member

Member

9 points

21 Posts

Re: Re: Re: Custom date format in DatePicker in Beta 2.0

I've found the answer why setting the ShortDatePattern = "dd/MMM/yyyy" does not work.  It turns out that it needs to have the escape character "\" the for slash "/" in the format string.  Once I change to the following, it works

Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = "dd\/MMM\/yyyy";

 Thanks

David

Bhagirath Patadia
Bhagira...

Participant

Participant

1244 points

214 Posts

Re: Re: Re: Custom date format in DatePicker

 Addition to above and making it working

 public App()
        {
            this.Startup += this.Application_Startup;
            this.Exit += this.Application_Exit;
            this.UnhandledException += this.Application_UnhandledException;
            Thread.CurrentThread.CurrentCulture = (CultureInfo)Thread.CurrentThread.CurrentCulture.Clone();
            Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";

            InitializeComponent();
        }

 

public UserControl1()
        {
            // Required to initialize variables
            InitializeComponent();
            dt.Loaded += new RoutedEventHandler(dt_Loaded);
            dt.SelectedDateChanged += new EventHandler<SelectionChangedEventArgs>(dt_SelectedDateChanged);
        }

        void dt_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            this.dt.Text = this.dt.SelectedDate.Value.ToString("dd-MM-yyyy");
        }

        void dt_Loaded(object sender, RoutedEventArgs e)
        {
            this.dt.Text = DateTime.Now.ToString("dd-MM-yyyy");
        }

If this matches your answer then MARK IT AS ANSWER

kranthireddyr
kranthi...

Member

Member

2 points

4 Posts

Re: Custom date format in DatePicker in Beta 2.0

Add this in app.xaml.cs. 



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



Make sure that you add two namespaces

1. using System.Threading

2.Using System.globalization


It worked for me. could take a breather.....


Mark it as an answer if it server. :)

  • Unanswered Question
  • Answered Question
  • Announcement