How to get DateTime.ToString() to use user preferences?

  • Thread starter Thread starter Roy Chastain
  • Start date Start date
R

Roy Chastain

I am trying to format a DateTime using ToString(). I am using the "t" formating for short time. The system is configured to use
HH.mm.ss but the result formatted time is in the default short time of h.mm tt. I have tried passing in a DateTimeFormatInfo that
was returned from DateTimeFormatInfo.CurrentInfo, but that did not work.

Bottom line is that I don't have any idea how to do this. I expect that it is simple once I know the plan.

Anyone have code to get this right they could share?

Thanks
 
I am sorry, but that is nowhere near the answer to the question asked.

I asked how to get it to format according to the USER's preferences. I know how to do it by hard coding the format.
 
This should do if for you:

DateTime.Now.ToString(Thread.CurrentThread.CurrentCulture)

The resultant string format is influenced by the settings in the Regional Options control panel.

HTH, Metallikanz!
 
DateTime.Now.ToString(Thread.CurrentThread.CurrentCulture) does produce a string with the date and time in the format specified in
the Regional Options. That gets me closer. Thank you.

However, I am still having a problem.
I tried DateTime.Now.ToString("t",Thread.CurrentThread.CurrentCulture) because I just wanted the time portion and I expected that
it would display the time in the format specified in the Regional Options, but instead it continues to display h.mm tt.

t format says, it is associated with the ShortTimePattern of the DateTimeFormatInfo

Is this a bug, bad documentation, wishful thinking on my part, or fill in the blank?

Thanks
 
Hi

Based on my test, the culture will work as expected.
Here is my test sample.

MsgBox(Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortTimePattern)
'current format H:mm:ss
MsgBox(DateTime.Now.ToString("t",
Thread.CurrentThread.CurrentCulture)) 'Here the time will be H:mm:ss format
Dim o As CultureInfo = Thread.CurrentThread.CurrentCulture.Clone()
o.DateTimeFormat.ShortTimePattern = "h:mm:ss"
Thread.CurrentThread.CurrentCulture = o

MsgBox(Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortTimePattern)
MsgBox(DateTime.Now.ToString("t",
Thread.CurrentThread.CurrentCulture)) 'Here the time will be h:mm:ss
format.

So I think you may try to check the
Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortTimePattern.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
I have looked at the ShortTimePattern in the returned culture and yes the output matches it.

The question is, how do I get ToString to format a date/time the way the customer wants it formatted? (Note in this case, the way
the customer wants it formatted is the way he defined in the regional settings.)

Is this something as simple as a problem with the words Short and Long when applied to time patterns. I noticed that there are
also long time patterns and they seem to have hh instead of h. Could it be that the regional settings are really showing up in
the long time pattern instead of the short one. (Long and short dates make sense to me - Long and short times do not - At least
not in US-english)

Thanks
 
Hi

If you have a look at the regional setting, you will find that there is
short date, long date and a time. But there is no a short/long time setting.

So it seems that the .net framework will map the short date, long date. Map
the long time to the time in the regional setting.
It did not map the short time, because there is no such one.

Try to run the code below.
'Four date/time formatting
MsgBox(DateTime.Now.ToShortTimeString())
MsgBox(DateTime.Now.ToLongTimeString())
MsgBox(DateTime.Now.ToShortDateString())
MsgBox(DateTime.Now.ToLongDateString())

If you run the code below, you will find if you change the time in the
regional setting, the long time will change. But the shorttime and short
time pattern will not change. But the ToShortTimeString and
DateTime.Now.ToString("t") accord with ShortTimePattern.


MsgBox(Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortTimePattern.T
oString())
MsgBox(DateTime.Now.ToString("t"))

MsgBox(Thread.CurrentThread.CurrentCulture.DateTimeFormat.LongTimePattern.To
String())

So for your scenario, I think you may try to use the long time instead, if
necessary you can exact the short time from the long time string.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi

Thanks for your reply and glad to see that you have worked out.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top