Page view counter
Custom date format in DatePicker in Beta 2.0 Subscribe to this thread
Last post 03-30-2009 10:14 AM by David H. 7 replies.
Sort Posts:
11-13-2008 1:58 AM
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 

pthakkar

Loading...
Joined on 11-13-2008
Posts 4
11-13-2008 2:02 AM
Marked as Answer
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

preishuber

Loading...
Joined on 09-20-2002
Austria/Germany
Posts 434
11-17-2008 3:16 AM
Marked as Answer
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.

Amanda Wang - MSFT

Loading...
Joined on 04-30-2007
Posts 1,015
11-21-2008 1:56 AM
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

jcmoore0

Loading...
Joined on 08-25-2008
Posts 4
12-11-2008 4:57 AM
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
virendrakr

Loading...
Joined on 12-11-2008
Posts 3
01-05-2009 2:18 AM
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.

Amanda Wang - MSFT

Loading...
Joined on 04-30-2007
Posts 1,015
03-27-2009 9:42 AM
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

Loading...
Joined on 03-13-2009
Posts 13
03-30-2009 10:14 AM
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

David H

Loading...
Joined on 03-13-2009
Posts 13
Microsoft Communities