473,598 Members | 2,916 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.D ateTimeFormat.S hortDatePattern = "MM/dd/yyyy"
customCulture.D ateTimeFormat.S hortTimePattern = "HH:mm:ss"
Thread.CurrentT hread.CurrentCu lture = 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 1505
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.D ateTimeFormat.S hortTimePattern = "HH:mm:ss"

With:

customCulture.D ateTimeFormat.L ongTimePattern = "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.D ateTimeFormat.S hortDatePattern = "MM/dd/yyyy"
customCulture.D ateTimeFormat.S hortTimePattern = "HH:mm:ss"
Thread.CurrentT hread.CurrentCu lture = 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
2405
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 narrowed it down to the Scrubber.py file.. If this seems like a quick step me through, I would be very appreciative, could get you something on your Amazon wish-list (that is me on my knees begging).. From just my basic understanding, it...
1
17333
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 date columns) if the row is being inserted and set a column (e.g. just the modified date column) if the row is being updated. I know how to do this in oracle plsql. I would define it as a before insert or update trigger and reference old and new...
3
1687
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 calling the same method same way except for creating object instead directly calling the method? i get this when trying to do that:- ************************************************** Response is not available in this context. Description: An...
2
1727
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 variables, formats a header, and assigns a sql stored procedure to be run and a report generated in a web browser. The odd behavior occurs where I have my breakpoint, which is at the place where the variable "showblank" is set to = "&nbsp;". ...
6
6087
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 separate instance of IE. Is this possible, keeping in mind that I cannot add anything to the registry, since a user may still use IE to visit the web app.
0
1094
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 DetailsView (Insert mode) in ASP.NET 2.0 and one of the fields I give a default value. <asp:DetailsView ....> <asp:TemplateField HeaderText="Date Entered" SortExpression="d_entered">
1
2781
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, which will result in saving it. Saving files during the build process is dangerous and can result in incorrect build outputs in future. Do you want to continue with the check out?
1
1496
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 REP. I need help first on modify the stored proc below. I must modify the procedure below and the two tables attached as code snippet can help get the REP_NAME. ===============================================================================...
9
2072
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 behavor? I'd like that every exception raised from ASP.NET runtime during an asyncpostback is managed in client side with a popup that reports the error message. thanks
0
7981
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8392
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8046
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6711
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5847
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3894
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3938
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2410
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1500
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.