DateTime.Now.ToString("t") not printing what I expect

  • Thread starter Thread starter Eric Renken
  • Start date Start date
E

Eric Renken

In my code I want to just display the time so I use the text formater "t" to
display the short time format.

I have my computer setup to display time in 24 hour format; however this
still displays time as "7:14 PM", and I would expect it to display "19:14".

My reginal settings, "Standards and formats" is set to "English (United
States)", but I customized it to put the Time is 24 hour format. I am using
..net 2.0, and this really feels like a bug in the framework to me as it
isn't pulling the correct time formating.

Eric
 
Well I can't use these properties becuase it is displaying time in another
control or in a report, but I did do a test and I don't think those work
correctly either:

From the command window I tested:

? DateTime.Now.ToShortTimeString()
"9:52 PM"

? DateTIme.Now.ToLongTimeString()
"21:52:42"

I would think that ToShortTimeString() which should be equaly to "t" should
give me "21:52", but it doesn't.

Eric
 
Eric said:
In my code I want to just display the time so I use the text formater "t" to
display the short time format.

I have my computer setup to display time in 24 hour format; however this
still displays time as "7:14 PM", and I would expect it to display "19:14".

My reginal settings, "Standards and formats" is set to "English (United
States)", but I customized it to put the Time is 24 hour format. I am using
..net 2.0, and this really feels like a bug in the framework to me as it
isn't pulling the correct time formating.

Note that the DateTimeFormat class includes *two* members relating to
time patterns: ShortTimePattern and LongTimePattern. Seeing that, I
tried an experiment. Using Control Panel, I set my computer to en-US,
but with a customised time format. I then ran this code:

Dim currentculture As CultureInfo = CultureInfo.CurrentCulture
Dim enus As CultureInfo = CultureInfo.GetCultureInfo("en-US")

Console.WriteLine(currentculture.DateTimeFormat.ShortTimePattern)
Console.WriteLine(enus.DateTimeFormat.ShortTimePattern)

Console.WriteLine(currentculture.DateTimeFormat.LongTimePattern)
Console.WriteLine(enus.DateTimeFormat.LongTimePattern)

Console.WriteLine(t.ToString("t", currentculture))
Console.WriteLine(t.ToString("t", enus))
Console.WriteLine(t.ToString("T", currentculture))
Console.WriteLine(t.ToString("T", enus))


The output was:

h:mm tt
h:mm tt
HH:mm:ss
h:mm:ss tt
2:21 PM
2:21 PM
14:21:13
2:21:13 PM

Which suggests to me that when you manipulate the tim format in the
Control Panel applet, you are actually modifying the LongTimePattern of
the current culture. Which is why your ToString("t") - asking for time
in the ShortTimePattern - is unchanged.

Whether there is a way to customise the ShortTimePattern, I don't know.
 
Hi Eric,

Thank you for your post.

Based on my understanding, your question is how to display time in 24 hours
format. If I've misunderstood anything, please feel free to post here.

You can use "HH:mm" to format the output in 24 hours format. Fore more
information, please refer to following article:

#Custom DateTime Format Strings
http://msdn2.microsoft.com/en-us/library/8kb3ddd4.aspx

Hope this helps. Please feel free to post here if anything is unclear.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
I don't want to force a 24hour format. I want it to follow what they have
set for the Time format in the control panel. Like Larry said changing the
time format in the control panel seems to change the Long Time format, but
it doesn't appear to change the short time format. Seems to me that it
should change both formats.

I could enter "HH:mm", but I want it to follow the current culture settings.
I could build that into my application and let them change it there, but
that doesn't seem like that would flow with Windows Usability Requirements.
That is where I would think the "t" would handle this for me.

Eric Renken
 
Thanks Larry for your input.

Hi Eric,

As Larry tested in the code, the ShortTimePattern is not read from the
Control Panel's Regional and Language Options. The time format is actually
LongTimePattern's default value in .NET DateTimeFormatInfo. You can get all
supported cultures' ShortTimePattern by following code:

foreach(CultureInfo ci in
CultureInfo.GetCultures(CultureTypes.AllCultures))
{
if (!ci.IsNeutralCulture)
Console.WriteLine("{0}\t{1}\t{2}", ci.Name,
ci.DateTimeFormat.ShortTimePattern, ci.DateTimeFormat.LongTimePattern);
}

The DateTime format set in Control Panel's Regional and Language Options is
user-specific, the settings can be found at the user's registry key
"HKEY_CURRENT_USER\Control Panel\International", you will find
"sShortDate", "sLongDate", "sTimeFormat", etc.

Based on what account your ASP.NET web application is running at, the
account's settings will be used as default date time format
(LongTimePattern, LongDatePattern, ShortDatePattern, etc). If you are using
the File System mode to run/debug web application, it's running using your
current logon user account, thus takes current regional settings in control
panel into account. If you're using IIS mode to run/debug web application,
it's running using ASP.NET worker process account ('NETWORK SERVICE' if on
IIS6/Win2003, 'ASPNET' if on IIS5.x), thus only those accounts' settings
will be used as the default values.

This is all about using default settings, you can override this behavior by
explicitly set a culture in machine.config or web.config:

<globalization culture="en-US" ...

I'm not sure by means of "I want it to follow the current culture
settings", do you mean use the current culture on the system which is
running the ASP.NET web application, or the client that is visiting your
web application? Because ASP.NET 2.0 has a great feature called "auto
culture", when turned on, each user visiting your web application will use
his/her culture settings when browsing your web application. To turn on
this feature, either in web.config:

<globalization culture="auto" ...

or in individual page directive:

<@ page culture="auto" ...

By the way, if you want to know which regional settings that the 'NETWORK
SERVICE' or 'ASPNET' account is using (since you cannot logon locally using
these accounts), you can first find its SID using PsGetSid and look at
"HKEY_USERS\<sid>\Control Panel\International".

#PsGetSid
http://www.sysinternals.com/Utilities/PsGetSid.html
Use 'psgetsid ASPNET' or 'psgetsid "Network Service"' to get the SID of
ASP.NET worker process account.

Hope this helps. Please feel free to post here if anything is unclear.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
OK, I'm not running a ASP.NET application. I am just running a Windows
application that has many grids and uses DataDynamics ActiveReports. In a
Windows application I really expect that "t" should display "21:00" for 9 PM
if that is what they set. It is fine that it is user specific that is
exactly what I would want. Sorry for leading you into thinking I was using
a ASP.NET application, but this is a Windows application sold to customers.

Eric Renken
 
Hi Eric,

Sorry for the misunderstanding that you're using ASP.NET.

It's unfortunate that Control Panel's Regional and Language Options only
specifies the long time format.

If your requirement is to know whether the user specifies 24-hours or
12-hours format in Regional Options, I think we can use a workaround to
know that from DateTimeFormatInfo.LongTimePattern:

static void Main(string[] args)
{
string stf =
GetShortTimePatternFromLongTimePattern(Thread.CurrentThread.CurrentCulture.D
ateTimeFormat);
Console.WriteLine(DateTime.Now.ToString(stf));
}

public static string
GetShortTimePatternFromLongTimePattern(DateTimeFormatInfo dtfi)
{
string ltf = dtfi.LongTimePattern;
string ltf_U = ltf.ToUpper();
int index_h = ltf_U.IndexOf('H');
int index_m = ltf_U.LastIndexOf('M');
if (!(index_h == -1 || index_m == -1 || index_h > index_m))
{
return ltf.Substring(index_h, index_m - index_h + 1);
}
return dtfi.ShortTimePattern;
}

Hope this helps. Please feel free to post here if anything is unclear.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Boy that will work but is that ever ugly. I am going to post this as a
bug/feature request.

Eric Renken
 
Back
Top