This is a migrated thread and some comments may be shown as answers.

Odd problem parsing dates

14 Answers 97 Views
Input
This is a migrated thread and some comments may be shown as answers.
Thomas
Top achievements
Rank 1
Thomas asked on 23 Jul 2009, 12:49 AM
I'm running into an odd issue parsing dates when I set the culture. Here's the repro steps
1. Create a blank Telerik Web Application
2. On the Default.aspx page drop a RadScriptManager and a RadDatePicker
3. In the code behind, add the following method:

protected override void InitializeCulture() 
    base.InitializeCulture(); 
 
    var culture = CultureInfo.CurrentCulture.Clone() as CultureInfo; 
    if ( culture == null ) 
        return
 
    culture.DateTimeFormat.ShortDatePattern = "dd-MMM-yyyy"
 
    this.Culture = culture.ToString(); 
    this.UICulture = culture.ToString(); 
    Thread.CurrentThread.CurrentCulture = culture; 
    Thread.CurrentThread.CurrentUICulture = culture; 
 


Notice that all I'm doing is cloning the existing Culture (which in my case happens to be en-US) and changing the default ShortDateTimePattern.

The effect of doing this is that the control will parse dates as if they are European dates instead of US dates. For example, entering 05012009 will result in 05-Jan-2009 instead of 01-May-2009.

14 Answers, 1 is accepted

Sort by
0
Thomas
Top achievements
Rank 1
answered on 23 Jul 2009, 04:29 PM
In the off chance that the answer to my question is something akin to the fact that parser uses the ShortDatePattern for parsing, how do I globally set the DisplayDateFormat for all date controls in my application at runtime (I'm fully aware that I can use skins to do it at design time)?
0
Georgi Krustev
Telerik team
answered on 24 Jul 2009, 03:15 PM
Hello Thomas,

I have answered to your support thread opened on the same matter. Here is a quote of the answer:

Hello Thomas,

Thank you for contacting us.

Unfortunately there is no easy way how to attain the required functionality. One possible solution is to override RadDatePicker and set DisplayDateFormat property. Hence you will need to use everywhere this control in your application. Thus the DisplayDateFormat will be set for each control used in the app. Nevertheless if you need to change the DisplayDateFormat depending on some condition, you still will need to set it for the control placed on the current page.

Let me know whether this helps.

All the best,


Sincerely yours,
Georgi Krustev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Thomas
Top achievements
Rank 1
answered on 24 Jul 2009, 06:33 PM
Am I to also assume that the CultureInfo's DateTimeFormat.ShortDatePattern property is used in the parsing algorithm? That's a shame because the solution seems intuitive IMO but I can understand how that would cause a problem.

Unfortunately, this puts me into a rather ugly lurch as our application is used by numerous customers in multiple countries that themselves have offices in multiple countries where they want to standardize the date display which is why a skin won't work unless I jump through some serious hoops a try writing the skin file at runtime (bleh). It's that, or replace every single date control in the system with a derived version.
0
Nikolay Rusev
Telerik team
answered on 30 Jul 2009, 07:17 AM
Hello Thomas,

I am afraid that the options you have is then one suggested by my colleague Georgi in his previous replay. However i will forward your feedback to our developers.

Regards,
Nikolay
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Thomas
Top achievements
Rank 1
answered on 24 Aug 2009, 11:31 PM
I've come back to this issue on the date formats. In general, it is a PITA that there is no way to globally set the date format at run time. In fact, I ran into an interesting issue with changing the format. I notice that changing the format which is set at runtime does not take effect if you are using an expression builder and have compiled the app with aspnet_compiler. The only way I was able to force the effect to occur is by recompiling the app using aspnet_compiler.

I do not want to make a derived date control because there is plenty of subitems on the date control that I don't want to have to implement. It makes for a brittle solution.

I seem to remember way back in the day, it used to be possible to set the date format in the web.config file. Even that would be much nicer as I have many clients running off the same code base but with different parent web.config files.

In the end, we're going to have to set the date format using the code we use for security which enables/disables all the controls on the page. Even that may not work entirely as some of the controls may be buried in placeholders, grids etc. Not sure but I'll post again when I know.
0
Thomas
Top achievements
Rank 1
answered on 25 Aug 2009, 12:16 AM
As another follow up, we basically ended up having to set the DateFormat and DisplayDateFormat on each control manually (i.e. in markup everywhere). In doing so, we realized that setting these values probably does nothing to the format of the calendar, which I believe will use the page's culture (and thus the browser's by default) for formatting. I also note that I cannot set the calendar's culture in javascript which would have helped.
0
Georgi Krustev
Telerik team
answered on 27 Aug 2009, 03:33 PM
Hello Thomas,

The only way to set the culture of the control is described in these online help articles:
http://www.telerik.com/help/aspnet-ajax/calendar_localizationcultureinfo.html
http://www.telerik.com/help/aspnet-ajax/calendar_localizationdateformatpattern.html

Nevertheless I will follow-up your suggestions to our developers for further consideration.

Regards,
Georgi Krustev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Thomas
Top achievements
Rank 1
answered on 27 Aug 2009, 05:52 PM
Am I correct in assuming that the DisplayDateFormat and DateFormat override the Culture even if all three are set in code?
0
Thomas
Top achievements
Rank 1
answered on 27 Aug 2009, 05:55 PM
Btw, the date format pattern won't work without also setting the culture as that creates the same problem: the format is based on the user's browser culture. The scenario we have is that some of our customers have offices in multiple parts of the world and want the same format used for display and entry at all offices and the display format is not always standard. With many dozens of date controls, it is real problem to control the display and entry settings globally across the app.
0
Georgi Krustev
Telerik team
answered on 31 Aug 2009, 04:36 PM
Hello Thomas,

The expected behavior of the calendar is to get the Culture from the page. If you explicitly set the culture of the control, the page's Culture will be overridden. If you set just the date format the input control will use the culture of the page. You mentioned for the DateFormat, DisplayDataFormat in one of your earlier posts. In general these properties uses the current culture to format the date, they do not override it.

I suppose that in order to achieve your goal you will need to set culture property of the Page. Thus offices around the world will open page with explicitly set culture.

Regards,
Georgi Krustev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Thomas
Top achievements
Rank 1
answered on 31 Aug 2009, 05:17 PM
I tried setting the culture of the page. That should solve the issue with the calendar but not the display format issue. I.e., I still have to add markup to every DatePicker control to explicitly set the display format if the user desires something different than default for the given culture.
0
Georgi Krustev
Telerik team
answered on 01 Sep 2009, 02:04 PM
Hello Thomas,

I hope you will agree with me, that if the requirement is to show the date  in different than the set culture it is required to set the DateFormat and the DisplayDateFormat. Unfortunately you will need to set them declaratively for every calendar.

Regards,
Georgi Krustev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Thomas
Top achievements
Rank 1
answered on 01 Sep 2009, 03:07 PM
I do agree and therein is the issue. I have many dozens of date controls in my app. Most customers want a display date format of dd-MMM-yyyy for clarity when dealing with multiple cultures to avoid the confusion between m/d/yyyy and d/m/yyyy. The problem is that this obviously deviates from the ShortDatePattern on any culture. I want the ability to globally set that format in the web.config file or barring that at runtime. I cannot set it in a Theme because there are times when clients want to change that (e.g. some do want m/d/yyyy, some m/d/yy etc) and with many hundreds of clients, I want (and have) a single code base.
0
Georgi Krustev
Telerik team
answered on 04 Sep 2009, 02:26 PM
Hello Thomas,

You can set a value for the date format in the web.config file. Hence you can retrieve it for every RadDatePicker control and to set it. Thus you can achieve global assigning of the DateFormat property of the RadDatePicker.

Best regards,
Georgi Krustev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Input
Asked by
Thomas
Top achievements
Rank 1
Answers by
Thomas
Top achievements
Rank 1
Georgi Krustev
Telerik team
Nikolay Rusev
Telerik team
Share this question
or