Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2005

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Roy Chastain - 17 Aug 2005 12:43 GMT
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
-------------------------------------------
Roy Chastain
KMSYS Worldwide, Inc.
http://www.kmsys.com
AMercer - 17 Aug 2005 21:07 GMT
Try something like:
 Now.ToString("HH.mm.ss")

> 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
[quoted text clipped - 9 lines]
> KMSYS Worldwide, Inc.
> http://www.kmsys.com
Roy Chastain - 18 Aug 2005 12:00 GMT
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.

>Try something like:
>  Now.ToString("HH.mm.ss")
[quoted text clipped - 12 lines]
>> KMSYS Worldwide, Inc.
>> http://www.kmsys.com

-------------------------------------------
Roy Chastain
KMSYS Worldwide, Inc.
http://www.kmsys.com
Metallikanz! - 18 Aug 2005 18:13 GMT
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!

>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
[quoted text clipped - 9 lines]
> KMSYS Worldwide, Inc.
> http://www.kmsys.com
Roy Chastain - 18 Aug 2005 18:45 GMT
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

>This should do if for you:
>
[quoted text clipped - 17 lines]
>> KMSYS Worldwide, Inc.
>> http://www.kmsys.com
-------------------------------------------
Roy Chastain
KMSYS Worldwide, Inc.
http://www.kmsys.com
"Peter Huang" [MSFT] - 19 Aug 2005 07:05 GMT
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

Signature

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

Roy Chastain - 19 Aug 2005 12:20 GMT
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
>
[quoted text clipped - 24 lines]
>Get Secure! - www.microsoft.com/security
>This posting is provided "AS IS" with no warranties, and confers no rights.
-------------------------------------------
Roy Chastain
KMSYS Worldwide, Inc.
http://www.kmsys.com
"Peter Huang" [MSFT] - 22 Aug 2005 13:38 GMT
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

Signature

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

Roy Chastain - 22 Aug 2005 14:54 GMT
Thanks
I had come to that conclusion.  Thanks for confirming.

>Hi
>
[quoted text clipped - 35 lines]
>Get Secure! - www.microsoft.com/security
>This posting is provided "AS IS" with no warranties, and confers no rights.
-------------------------------------------
Roy Chastain
KMSYS Worldwide, Inc.
http://www.kmsys.com
"Peter Huang" [MSFT] - 23 Aug 2005 07:34 GMT
Hi

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

Best regards,

Peter Huang
Microsoft Online Partner Support

Signature

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.