Modify default behavior of Date.ToString?

T

Tom

I need to modify the default behavior of Date.ToString() on all pages
of my ASP.Net (2.0) site. I don't need to localize my app (it's an
intranet-only site), but I need to enforce a specific date/time format
sitewide ("MM/dd/yyyy HH:mm:ss") and I don't want to have to write that
each time I have to format a date to string.

I googled CurrentCulture and Date Format, and I found some articles on
customizing the default culture of your machine via the control panel -
that didn't work. Eventhough I was able to set it, it did not get
applied to dates on ASP.Net pages.

Then I tried setting the CurrentCulture for the current thread to a
customized culture object:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
EventArgs) Handles Me.Load

Dim customCulture As CultureInfo = New CultureInfo("en-US")
customCulture.DateTimeFormat.ShortDatePattern = "MM/dd/yyyy"
customCulture.DateTimeFormat.ShortTimePattern = "HH:mm:ss"
Thread.CurrentThread.CurrentCulture = customCulture

End Sub

That works (sort of), but there are 2 problems:

1) Date.ToString() still renders an AM/PM designator which is not
needed b/c I'm using 24hr time
2) I can put this code into my base page class, but do I really want to
execute this code everytime a page loads? I'd rather set it somewhere
lower in the stack.

Any help would be appreciated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top