We help IT Professionals succeed at work.

How to force a time format regardless of regional settings.

PatrickJacques
on
Medium Priority
286 Views
Last Modified: 2012-08-14
Hi, I'm trying to write a program that reads timeframes from an ini file and after a change by the user, it saves back the information to the ini file. The thing is that I want to keep a constant format so that it will always read the right information. This is how I want the ini file :

[Time]
Dim=02:20:00 AM-08:00:00 PM
Lun=02:20:00 AM-09:00:00 PM
Mar=03:20:00 AM-10:00:00 PM
Mer=04:20:00 AM-11:00:00 PM
Jeu=05:20:00 AM-01:00:00 PM
Ven=06:20:00 AM-02:00:00 PM
Sam=07:20:00 AM-03:00:00 PM

so that when I read the file I can set the dateTimePicker properly using this :

Case currentline.Contains("Dim=")

                                timeSunBegin.Text = currentline.Substring(4, 11)
                                timeSunEnd.Text = currentline.Substring(16, 11)

The problem is that if my regional settings is set to h:mm:ss tt it won't show me the first digit if it's a zero. So when I write back to the ini file, it misses one digit.... I would like to force a format so that I keep the first digit regardless of what is set in the regional settings of the computer.

Any ideas?? Thanks
Comment
Watch Question

Author

Commented:
By the way, I already tried to use String.Format(timeSunBegin.Text, "hh:mm:ss tt") when I write back to the file, but it doesn't do the trick, it still shows me 2:20:40 AM instead of 02:00:40 AM

Thanks
You can set the local settings for your app environment by using the globalization namespaces.

Author

Commented:
Can you clarify? I never used this function before..

Thanks for your help.
date.now.tostring("HHMMss")

'your current settings
  MessageBox.Show(CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern)
'your time with your current settings
  MessageBox.Show(Now.ToShortTimeString())
'clone your culture
    Dim ci As CultureInfo = Application.CurrentCulture.Clone
'modify it
  ci.DateTimeFormat.ShortTimePattern = "hh:MM:ss"
   Application.CurrentCulture = ci

'did it work?
       MessageBox.Show(Now.ToShortTimeString())
You also can override different properties that way, and your application will keep on using it, as long as you stay in the same thread

Author

Commented:
Thanks PockyMaster, I think we're getting close to it, but still does not work. Here's what I have :

        Dim ci As CultureInfo = Application.CurrentCulture.Clone
        ci.DateTimeFormat.ShortTimePattern = "hh:MM:ss tt"
        Application.CurrentCulture = ci


        'Update TimeFrame
            CompleteText = CompleteText.Replace(CompleteText.Substring(72, 23), timeSunBegin.Text + "-" + timeSunEnd.Text)
            MsgBox(timeSunBegin.Text)
            MsgBox(CompleteText)

and the first msgbox shows me 2:20:40 AM instead of 02:20:40.

Thanks

Author

Commented:
I guess I have to format my DateTimePicker too.. but I don't know how.. it does not show the first digit. Is there a way to force it ? I saw the customFormat property but does not do anything.

Thanks
Let me try it for you..wait a mom

Author

Commented:
God dawm it.. I think I just found out...newbie issue, sorry for the disturbance.

Author

Commented:
I simply had to select Custom in the Format of the DateTimePicker instead of Time....
There you go :D

Also use the Value instead of Text (that will provide you with a DateTime obj, instead of String)

Author

Commented:
In what way is it better?
You will not lose the datatype until you present it to the user, in that way you can still decide at anytime to present it one time as a date, later as a time, or whatever format you like to use, but once it's a text.. you need to do some work to get back that information.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Alright, thanks I appreciate your help.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.