String ==> date conversions while overriding regional settings

  • Thread starter Thread starter Jamie Noble
  • Start date Start date
J

Jamie Noble

I am parsing a machine generated report that always produces a date
field in the following format:

MM/dd/yyyy

I need to do a number of date operations in a program. All other date
operations are basically just doing things like DateDifferences from
the original report date, datemin and datemax.

Here's partial code showing original date parsing.

sTempOrigDate = "02/25/2004"
sTempMonth = sTempOrigDate.Substring(0, 2)
sTempDay = sTempOrigDate.Substring(3, 2)
sTempYear = sTempOrigDate.Substring(6, 4)
sReportLDate = sTempDay & "/" & sTempMonth & "/" & sTempYear
dReportLDate = Convert.ToDateTime(sReportLDate)

The converted string to date is only used internally for date
calculations and is never used to display output.

I am using Visual Basic .NET

1) What I would prefer to do is simply tell my application to override
the regional settings and instead always use en_us which is in this
format I believe. How do I do this? I believe this format matches the
original report format which would eliminate the need to do the string
operations in the first place.

The reason I want to do this is to avoid any date conversion issues
caused by different regional settings set in control panel.

Can someone help with this. I've seen references to CultureInfo and
threads but I am not really sure if that's either the right way to go
or the easiest way to do this.

Help is appreciated!

~Jamie N
 
I hope this helps ...

System.Threading.Thread.CurrentThread.CurrentCulture= new
System.Globalization.CultureInfo("en-US");
 
Jamie said:
I am parsing a machine generated report that always produces a date
field in the following format:

MM/dd/yyyy

I need to do a number of date operations in a program. All other date
operations are basically just doing things like DateDifferences from
the original report date, datemin and datemax.

Here's partial code showing original date parsing.

sTempOrigDate = "02/25/2004"
sTempMonth = sTempOrigDate.Substring(0, 2)
sTempDay = sTempOrigDate.Substring(3, 2)
sTempYear = sTempOrigDate.Substring(6, 4)
sReportLDate = sTempDay & "/" & sTempMonth & "/" & sTempYear
dReportLDate = Convert.ToDateTime(sReportLDate)

The converted string to date is only used internally for date
calculations and is never used to display output.

I am using Visual Basic .NET

1) What I would prefer to do is simply tell my application to override
the regional settings and instead always use en_us which is in this
format I believe. How do I do this? I believe this format matches the
original report format which would eliminate the need to do the string
operations in the first place.

The reason I want to do this is to avoid any date conversion issues
caused by different regional settings set in control panel.

Can someone help with this. I've seen references to CultureInfo and
threads but I am not really sure if that's either the right way to go
or the easiest way to do this.

Depending on how strict you want (or need) to be, you should use one of
the following (watch for wrap):

DateTime.ParseExact( "03/12/2004", "MM/dd/yyyy",
CultureInfo.InvariantCulture)

or

DateTime.Parse( "3/12/2004", CultureInfo.CreateSpecificCulture(
"en-US"))

Using ParseExact will cause an exception if your day or month part of
the string does not have a leading zero when it's less than 10. Parse
is more forgiving.

Since these methods take the CultureInfo class as a parameter, there's
no need to change the thread's culture setting, even temporarily.
 
Back
Top