time format problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

I have changed my regional options on my computer to English (United States)
(I used to have Swedish). I would print the value of the current time in a
textbox on my asp.net page. I use this function:

txtTime.Text = DateTime.Now.ToShortTimeString();

The time shown on the page is on the format 16:34 and I dont want that. I
want the american format. What can be wrong?

Thanks
Julia
 
The code is run on the web server. If the server's regional setting is
Swedish, the text is still formatted in Swedish. It doesn't matter what
region the user is using.

If you want always display US format. You can
1) change the regional option of the server. or
2) format the time in us format (something like "hh:mm am/pm" but
not 100% sure). Or
3) sepcify the culture to get the time text.

John
 
Hi Julia,

That is odd. According to the .Net SDK regarding the return value of the
DateTime.ToShorTimeString method:

""The value of this instance is formatted using the short time format
character, 't'. The return value is identical to the value returned by
ToString ("t", null)."

This overload of the DateTime.ToString method takes 2 parameters, the second
of which is a System.Globalization.DateTimeFormatInfo instance. If it is
null, the DateTimeFormatInfo member of the static
System.GlobalizationCurrentCulture.CultureInfo property is used.

Again, according to the documentation, this Standard DateTime Format string
for the "en-us" culture specification is equivalent to the Custom DateTime
Format string "h:mm tt" (Hour 1-12, 2-digit Minute, and either "AM" or
"PM").

The CultureInfo.CurrentCulture class is documented as, when a managed
Application Thread starts, the Windows API GetUserDefaultLCID function is
called, which obtains the Local Identifier from the Operating System. This
Local Identifier is what you would set in Control Panel if you changed your
Regional Options.

The only possibility I can guess is that, since an ASP.Net application is
hosted in IIS, if you changed your Regional Options and did not either
restart IIS or reboot the computer, the ASP.Net application may not have
restarted, and fetched the new Local Identifier.

Here are some references that should help:

http://msdn2.microsoft.com/en-us/li...tion.datetimeformatinfo.shorttimepattern.aspx
http://msdn2.microsoft.com/en-us/library/system.globalization.cultureinfo.aspx
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/intl/nls_08tg.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/intl/nls_8sj7.asp

--
HTH,

Kevin Spencer
Microsoft MVP
Ministry of Software Development
http://unclechutney.blogspot.com

Never trust a dunderhead with a blunderbuss.
 
Hey Julia,

What you want to do is change the locale for the page to American
English just before you output the date.

So what you do is this:

System.Threading.Thread.CurrentThread.CurrentCulture = new
System.Globalization.CultureInfo("en-US");
txtTime.Text = DateTime.Now.ToShortTimeString();
 
Hi

and thanks for all the answers!

Finally I added these to my config file:

<globalization
requestEncoding="utf-8"
responseEncoding="utf-8"
culture="en-US "
uiCulture="en-US "
/>
This section will force the application to use the american time format, so
this will solve my problem.

The strange thing is that I run everything on my local machine (iis, sql,
client) and I did change my regional options to English but still the format
was in Swedish. Maybe I didnt restart my computer so I will try that today.

Thanks
Julia
 
Back
Top