Link to home
Start Free TrialLog in
Avatar of PatrickJacques
PatrickJacquesFlag for Canada

asked on

How to force a time format regardless of regional settings.

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
Avatar of PatrickJacques
PatrickJacques
Flag of Canada image

ASKER

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.

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
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
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
God dawm it.. I think I just found out...newbie issue, sorry for the disturbance.
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)
In what way is it better?
ASKER CERTIFIED SOLUTION
Avatar of PockyMaster
PockyMaster
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Alright, thanks I appreciate your help.