Skip to main content
Microsoft Silverlight
Home Forums General Silverlight Programming Programming with .NET - General Custom date format in DatePicker in Beta 2.0
9 replies. Latest Post by kranthireddyr on September 2, 2010.
(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
3572 points
656 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 ...
All-Star
17237 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
9 points
21 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";
Bhagira...
Participant
1244 points
214 Posts
02-24-2010 6:51 AM |
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"); }
kranthi...
2 points
09-02-2010 8:18 AM |
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. :)