PatrickJacques
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
[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
You can set the local settings for your app environment by using the globalization namespaces.
ASKER
Can you clarify? I never used this function before..
Thanks for your help.
Thanks for your help.
date.now.tostring("HHMMss" )
'your current settings
MessageBox.Show(CultureInf
'your time with your current settings
MessageBox.Show(Now.ToShor
'clone your culture
Dim ci As CultureInfo = Application.CurrentCulture
'modify it
ci.DateTimeFormat.ShortTim
Application.CurrentCulture
'did it work?
MessageBox.Show(Now.ToShor
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
ASKER
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.ShortTim ePattern = "hh:MM:ss tt"
Application.CurrentCulture = ci
'Update TimeFrame
CompleteText = CompleteText.Replace(Compl eteText.Su bstring(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
Dim ci As CultureInfo = Application.CurrentCulture
ci.DateTimeFormat.ShortTim
Application.CurrentCulture
'Update TimeFrame
CompleteText = CompleteText.Replace(Compl
MsgBox(timeSunBegin.Text)
MsgBox(CompleteText)
and the first msgbox shows me 2:20:40 AM instead of 02:20:40.
Thanks
ASKER
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
Thanks
Let me try it for you..wait a mom
ASKER
God dawm it.. I think I just found out...newbie issue, sorry for the disturbance.
ASKER
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)
Also use the Value instead of Text (that will provide you with a DateTime obj, instead of String)
ASKER
In what way is it better?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Alright, thanks I appreciate your help.
ASKER
Thanks