467,133 Members | 1,150 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,133 developers. It's quick & easy.

time format problem

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
Nov 23 '06 #1
  • viewed: 1902
Share:
5 Replies
"Julia" wrote:
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?
Set the ShortTimePattern property of the current culture thread:
http://msdn2.microsoft.com/en-us/lib...ng(VS.80).aspx
Nov 23 '06 #2
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
Julia wrote:
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
Nov 23 '06 #3
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/lib...mepattern.aspx
http://msdn2.microsoft.com/en-us/lib...ltureinfo.aspx
http://msdn.microsoft.com/library/de...l/nls_08tg.asp
http://msdn.microsoft.com/library/de...l/nls_8sj7.asp

--
HTH,

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

Never trust a dunderhead with a blunderbuss.
"Julia" <Ju***@discussions.microsoft.comwrote in message
news:3D**********************************@microsof t.com...
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

Nov 23 '06 #4
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.CurrentCultu re = new
System.Globalization.CultureInfo("en-US");
txtTime.Text = DateTime.Now.ToShortTimeString();

On Thu, 23 Nov 2006 07:40:02 -0800, Julia
<Ju***@discussions.microsoft.comwrote:
>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
--

Bits.Bytes.
http://bytes.thinkersroom.com
Nov 23 '06 #5
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

"Rad [Visual C# MVP]" wrote:
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.CurrentCultu re = new
System.Globalization.CultureInfo("en-US");
txtTime.Text = DateTime.Now.ToShortTimeString();

On Thu, 23 Nov 2006 07:40:02 -0800, Julia
<Ju***@discussions.microsoft.comwrote:
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
--

Bits.Bytes.
http://bytes.thinkersroom.com
Nov 24 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by David Graham | last post: by
10 posts views Thread by scorpion53061 | last post: by
1 post views Thread by OleMacGeezer | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.