string converstion to a date

  • Thread starter Thread starter Chuck Hecht
  • Start date Start date
C

Chuck Hecht

Hello,
I am trying to convert a string ("080714") to a date 08/07/14 using the format yyMMdd

mydate is declared as a date
grpod_date is the string 080714

I am getting the error "A first chance exception of type 'System.FormatException' occurred in mscorlib.dll"
on the following line.
mydate = DateTime.ParseExact(gProd_date, "yyMMdd", New Globalization.CultureInfo("en-US"))

any guidance would be appreciated

Thank you

chuck
 
Chuck Hecht skrev:
Hello,
I am trying to convert a string ("080714") to a date 08/07/14 using the
format yyMMdd

mydate is declared as a date
grpod_date is the string 080714

I am getting the error "A first chance exception of type
'System.FormatException' occurred in mscorlib.dll"
on the following line.

mydate = DateTime.ParseExact(gProd_date, "yyMMdd", New
Globalization.CultureInfo("en-US"))

any guidance would be appreciated
080714
 
Chuck Hecht skrev:
Bjorn,
I am not able to see your reply ....
Sorry...

Try to use null instead of the Globalization parameter, - you don't need
it since you are parsing only numbers.

Anyhow: This should not be your problem.

Are you really sure that your input string is exactly in the format
"yyMMdd"?

Try to debug.
 
I tried null first and it casued other issues. Another blogger recommended
the Globalization parameter.
the "date" value is pulled form a scanned barcode and is only 6 or 8
numbers. The format is also provided
by the customer as yyMMdd or MMddyyyyy. Anyways do I need to manually insert
the "/" seperators and
have it in date form before I pass it? I have just been passing a string of
just numbers like 080716
 
Chuck Hecht skrev:
I tried null first and it casued other issues. Another blogger recommended
the Globalization parameter.
the "date" value is pulled form a scanned barcode and is only 6 or 8
numbers.

Well, I guess you hit the 8-numbers variant, trying to parse it as the
6-digit variant.

Why don't you just debug the code, seeing what the reason is for the crash?

Or use a try/catch to trap the error.

try
{
mydate = DateTime.ParseExact(gProd_date, "yyMMdd", null);
}
catch
{
MessageWindow.Show(String.Format("Got a crash parsing
gProd_date='{0}'", gProd_date ?? "<null>"));
}

The format is also provided
by the customer as yyMMdd or MMddyyyyy. Anyways do I need to manually insert
the "/" seperators and
have it in date form before I pass it? I have just been passing a string of
just numbers like 080716
No, that's why you are using the format parameter.
 
I have used the debugger thats how I have seen the error.
I have tried using NULL as you recommend and I get the following from vb
at design time.

"Null" is not declared. Null Constant is no longer supported; use
System.DBNull instead."

I tried System.dbnull but was told by vb that:

DBNULL is a type in System and can not be used as an expression.
 
Probably the best solution is to implement a IFormatProvider class to handle
your custom date which will then be abstracted away from your app. Search
MSDN for examples. Don't rely on the DateTime class to format your date
correctly, that input format is not US format.
 
Back
Top