Skip to main content

Microsoft Silverlight

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

3570 points

655 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.preishuber.net http://weblogs.asp.net/hpreishuber

Amanda Wang - MSFT
Amanda W...

All-Star

All-Star

17241 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 W...

All-Star

All-Star

17241 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

6 points

16 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

6 points

16 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

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities