Skip to main content
Home Forums Silverlight Programming Programming with .NET - General Custom date format in DatePicker in Beta 2.0
7 replies. Latest Post by David H on March 30, 2009.
(0)
pthakkar
Member
0 points
4 Posts
11-13-2008 1:58 AM |
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
Contributor
3570 points
655 Posts
11-13-2008 2:02 AM |
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
Amanda W...
All-Star
17241 points
1,466 Posts
11-17-2008 3:16 AM |
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.
jcmoore0
6 points
11-21-2008 1:56 AM |
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
virendrakr
3 Posts
12-11-2008 4:57 AM |
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
01-05-2009 2:18 AM |
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:
David H
16 Posts
03-27-2009 9:42 AM |
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.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?
David
03-30-2009 10:14 AM |
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";