Format dates in a databound repeater

Data Access Application Blocks Forum

Format dates in a databound repeater

Postby neilx on Wed Feb 27, 2008 12:53 pm

If I get a date from my database, how do I output it in a standard way in a databound control like a repeater:

Code: Select all
<ItemTemplate>
<asp:Label ID=\"aDate\" runat=\"server\" Text='<%# Eval(\"aDate\") %>' />
</ItemTemplate>


aDate.ToString(\"yyyy-MM-dd\") works fine in code behind, but not on an Eval-ed item.

This is a site-wide issue for me in that I want all dates from my database to be presented in iso format (mostly without the time), so doing it centrally would be good.
neilx
Sergeant
 
Posts: 28
Joined: Sun Sep 09, 2007 6:11 pm

Postby neilx on Wed Feb 27, 2008 5:00 pm

I have found the StringFormat property in the EasyObject.cs which is actual the Date Format. It is public so I can set it to \"yyyy-MM-dd\" to achieve what I want - and then use
Code: Select all
Eval(\"s_aDate\")
neilx
Sergeant
 
Posts: 28
Joined: Sun Sep 09, 2007 6:11 pm

Postby neilx on Wed Feb 27, 2008 10:44 pm

I have what seems to be a good solution for this that requires a patch for EasyObject.cs:

Change the property StringFormat to:

Code: Select all
public string StringFormat = CultureInfo.CurrentUICulture.DateTimeFormat.ShortDatePattern;


There is a rogue ToString(\"MM/dd/yyyy\") in GetItemString() which I guess needs to be changed to use this.StringFormat.

Then either set the server ShortDate pattern as you want or in the application create a clone of the CurrentUICulture and set the ShortDatePattern there before resetting the current thread's CurrentUICulture:

Code: Select all
CultureInfo vNewCulture = (CultureInfo);
Thread.CurrentThread.CurrentUICulture.Clone();
vNewCulture.DateTimeFormat.ShortDatePattern = \"yyyy-MM-dd\";
vNewCulture.DateTimeFormat.DateSeparator = \"-\";
Thread.CurrentThread.CurrentUICulture = vNewCulture;

Kudos to Peter Blum for this last piece of code:
http://forums.asp.net/p/1223734/2190227.aspx
neilx
Sergeant
 
Posts: 28
Joined: Sun Sep 09, 2007 6:11 pm


Return to EasyObjects.NET (Microsoft Enterprise Library)

Who is online

Users browsing this forum: No registered users and 1 guest

cron