473,404 Members | 2,174 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Modify default behavior of Date.ToString?

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.

Aug 15 '06 #1
1 1494
Tom
Thanks to Scott Hansleman for this solution:

The default date format is "G", which according to MSDN is made up of
the short date and LONG time patterns:

http://msdn.microsoft.com/library/de...classtopic.asp

So all I had to do was replace:

customCulture.DateTimeFormat.ShortTimePattern = "HH:mm:ss"

With:

customCulture.DateTimeFormat.LongTimePattern = "HH:mm:ss"
And it worked.

Tom wrote:
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.
Aug 15 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Chris McKeever | last post by:
I am trying to modify the Mailman Python code to stop mapping MIME-types and use the extension of the attachment instead. I am pretty much clueless as to what I need to do here, but I think I have...
1
by: efinney | last post by:
Hi, I'm a newbie to sql server and this may be a really dumb question for some you. I'm trying to find some examples of sql server triggers that will set columns (e.g. the created and modified...
3
by: Farooq Khan | last post by:
why does Response.Write in a method of code-beind class when called from inpage code (i.e in <%---%>), after creating object of that class, fails when called while it works perfectly ok while...
2
by: | last post by:
I have a breakpoint in an aspx page that I'm using to try to trap some code to see what's going on. I'm translating a page that is working in a traditional ASP page, which takes several session...
6
by: Dave Slinn | last post by:
I have a VB app hosting the Webbrowser control. I would like to add "something" to the requests that app is submitted to our web application to indicate that its from this webbrowser and not a...
0
by: cjbland | last post by:
First off I apologize if this has been discussed before, I'm not certain how to search for what I'm looking for so I figured I'd throw this out there and see what I got. I am working with a...
1
by: =?Utf-8?B?Q2h1Y2sgUA==?= | last post by:
I have an asp.net solution with a web deployment project. When we try to build/debug, we get the message: An editor or project is attempting to check out a file that is modified in memory,...
1
by: macupryk | last post by:
Need to modify the stored proc where we can add the REP_NAME. to an existing crystal report. I will also need to order by REP_NAME and group by REP_NAME. Many customer id's can belong to one Sales...
9
by: Trapulo | last post by:
Hello, with ASP.NET 2.0 Ajax every unexpected error is managed client-side with a popup that reports the error to the user. In ASP.NET 3.5 this behavor has been changed: how can I have a similar...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.