Problem with regional date settings

S

Schfooge

I am trying to do a homework assignment, but I am having
troubles due to regional date settings.

Part of the program requires a date to be read from
a sequential file, which gives the date in string format.
The program needs to convert the string to a date.

However, the text file supplied is in US formatting
(eg. "07/24/2001" for July 24,2001 but my computer's
settings are Canadian (24/07/2001"). Now I know that
the easy way out would be to either change the text
file to Cdn format or my computer to US format, but
that will only fix the program so it works on computers
that have the same settings - which seems to me to be
sloppy programming - plus I don't know what the settings
are on the machine that my teacher will be testing my
program on.

What I need to know is whether or not there is any way
that I can force the program to use one country's
format over the other, regardless of the regional settings
of the computer it's running on.
 
C

Cor

Hi Schfooge,

Because it is a school project we do first a piece of analyse

You know the format of the string it is dd/mm/yyyy

You have to know the culture setting of the user computer and his date
format.

For that you can look here.

http://msdn.microsoft.com/library/d.../cpconformattingobjectsforspecificculture.asp

What I would do was just change the dd/mm in my string when I have readed it
when I know that it had to be mm/dd. Messing up with the localsettings can
give more problems, while this must be sufficient. (And write it back again
as dd/mm of course).

I hope this helps?

Cor
 
C

Cor

Hi Ken,

I had not yet readed your message when I was sending mine, so the words
"messing up with regional settings", was written and send before I had
readed your message. (Else I had written it in another way).

Before you would understand it wrong.

Cor
 
H

Herfried K. Wagner [MVP]

* "Schfooge said:
Part of the program requires a date to be read from
a sequential file, which gives the date in string format.
The program needs to convert the string to a date.

However, the text file supplied is in US formatting
(eg. "07/24/2001" for July 24,2001 but my computer's
settings are Canadian (24/07/2001"). Now I know that
the easy way out would be to either change the text
file to Cdn format or my computer to US format, but
that will only fix the program so it works on computers
that have the same settings - which seems to me to be
sloppy programming - plus I don't know what the settings
are on the machine that my teacher will be testing my
program on.

What I need to know is whether or not there is any way
that I can force the program to use one country's
format over the other, regardless of the regional settings
of the computer it's running on.

You can use 'DateTime.ParseExact' with a predefined format string.
 
S

Schfooge

Thanks for the quick responses, guys. I tried all three
suggestions, and they all worked fine. The solution
I settled on was to use

Imports System.Globalization

Dim culture As New CultureInfo("en-CA")
or... Dim culture As New CultureInfo("en-US")

DateTime.Parse(string, culture, DateTimeStyles.NoCurrentDateDefault)

I still haven't decided whether it's better to force the
Canadian or US formatting, but at least I know how
to force the program to stick to one format from
machine to machine.
 
H

Herfried K. Wagner [MVP]

* "Schfooge said:
Thanks for the quick responses, guys. I tried all three
suggestions, and they all worked fine. The solution
I settled on was to use

Imports System.Globalization

Dim culture As New CultureInfo("en-CA")
or... Dim culture As New CultureInfo("en-US")

DateTime.Parse(string, culture, DateTimeStyles.NoCurrentDateDefault)

I still haven't decided whether it's better to force the
Canadian or US formatting, but at least I know how
to force the program to stick to one format from
machine to machine.

If all the files are always formatted in this format, why not?
 
C

Cor

Hi Herfried,

He uses a textfile he said, so that is no problem.

Therefore my simple solution, but now I see he is not a junior scholar I
would take the one he sugested.

:)

Cor
 
H

Herfried K. Wagner [MVP]

* "Cor said:
He uses a textfile he said, so that is no problem.

Therefore my simple solution, but now I see he is not a junior scholar I
would take the one he sugested.

OK...
 
S

Schfooge

Cor said:
Hi Herfried,

He uses a textfile he said, so that is no problem.

Therefore my simple solution, but now I see he is not a junior scholar I
would take the one he sugested.

:)
I have a VB6 background, but I'm learning VB.NET at
my local community college. The course is a bit basic
(no pun intended), but it's a prereq to the more advanced
stuff.

It's just that I run into these Globalization issues because
I'm a Canadian using an American textbook that assumes
that all the readers are using the windows default
en-US culture.

Anyways, thanks again for the help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top