cmdolcet
asked on
String was not recognized as a valid DateTime
I am getting an error message saying that "Format Exception was unhandled".
I am trying to parse the string: 07/27/11 074854 using the following:
strdate = Date.Parse(Date_Time_Measu red).ToStr ing("yyyy MM dd HH:mm:ss")
How can I fix this? also I need to somewhat keep the DateTime parse the same because in newer files I parse out date time as 08/02/11 09:56:24
I am trying to parse the string: 07/27/11 074854 using the following:
strdate = Date.Parse(Date_Time_Measu
How can I fix this? also I need to somewhat keep the DateTime parse the same because in newer files I parse out date time as 08/02/11 09:56:24
try this:
Try
strdate = Date.Parse(Date_Time_Measu red).ToStr ing("MM/dd /yy HHmmss")
Catch ex As Exception
strdate = Date.Parse(Date_Time_Measu red).ToStr ing("MM/dd /yy HH:mm:ss")
End Try
Try
strdate = Date.Parse(Date_Time_Measu
Catch ex As Exception
strdate = Date.Parse(Date_Time_Measu
End Try
ASKER
No this gave me an error message String was not recognized as a valid DateTime
try this:
dim dt as Datetime
dt = DateTime.ParseExact(myds," MM/dd/yy HHmmss",CultureInfo.Invari antCulture )
and then use
dt.ToString("the format you like here")
for example
now.toString("MM/dd/YYYY HH:mm:ss tt")
dim dt as Datetime
dt = DateTime.ParseExact(myds,"
and then use
dt.ToString("the format you like here")
for example
now.toString("MM/dd/YYYY HH:mm:ss tt")
ASKER
OK what is myds and cultureInfo both are returning undeclaired
myds is the date string and use
System.Globalization.Cultu reInfo.Inv ariantCult ure
instead of
CultureInfo.InvariantCultu re
System.Globalization.Cultu
instead of
CultureInfo.InvariantCultu
ASKER
I get a FormatException was unhandled with the following code:
Dim dt As DateTime
dt = DateTime.ParseExact(Date_T ime_Measur ed, "MM/dd/yy HHmmss", System.Globalization.Cultu reInfo.Inv ariantCult ure)
Now.ToString("MM/dd/YYYY HH:mm:ss tt")
Dim dt As DateTime
dt = DateTime.ParseExact(Date_T
Now.ToString("MM/dd/YYYY HH:mm:ss tt")
>Now.ToString("MM/dd/YYYY HH:mm:ss tt")
Do you get the error on above line?
YYYY -> yyyy
Why do you have that line anyway?
Do you get the error on above line?
YYYY -> yyyy
Why do you have that line anyway?
ASKER
No I get the error on the following line:
dt = DateTime.ParseExact(Date_T ime_Measur ed, "MM/dd/yy HHmmss", System.Globalization.Cultu reInfo.Inv ariantCult ure)
dt = DateTime.ParseExact(Date_T
did you add this to your page
Imports System.Globalization
Imports System.Globalization
ASKER
Yes. I dont understand why the format "07/25/11 074854" doesn;t get recognized.
this is working fine for me
Imports System.Globalization
...
dim myDS as string = "07/27/11 074854"
now = DateTime.ParseExact(myds," MM/dd/yy HHmmss",CultureInfo.Invari antCulture )
response.write(now.toStrin g("MM/dd/Y YYY HH:mm:ss tt"))
Imports System.Globalization
...
dim myDS as string = "07/27/11 074854"
now = DateTime.ParseExact(myds,"
response.write(now.toStrin
oops, it should be yyyy
dim myDS as string = "07/27/11 074854"
now = DateTime.ParseExact(myds," MM/dd/yy HHmmss",CultureInfo.Invari antCulture )
response.write(now.toStrin g("MM/dd/y yyy HH:mm:ss tt"))
and result is
07/27/2011 07:48:54 AM
dim myDS as string = "07/27/11 074854"
now = DateTime.ParseExact(myds,"
response.write(now.toStrin
and result is
07/27/2011 07:48:54 AM
ASKER
When I use your code I get an error property NOW is ReadOnly ?????
use this (now was a just variable):
dim myDS as String = "07/27/11 074854"
dim myDT as DateTime = DateTime.ParseExact(myds," MM/dd/yy HHmmss",CultureInfo.Invari antCulture )
response.write(myDT.toStri ng("MM/dd/ yyyy HH:mm:ss tt"))
dim myDS as String = "07/27/11 074854"
dim myDT as DateTime = DateTime.ParseExact(myds,"
response.write(myDT.toStri
ASKER
OK It does work with your code above however when I use the following
dim Date_Time_Measured as string
Dim myDT As DateTime = DateTime.ParseExact(Date_T ime_Measur ed, "MM/dd/yy HHmmss", CultureInfo.InvariantCultu re)
strdate = myDT.ToString("MM/dd/yyyy HH:mm:ss tt")
I still get the same error. If I add this line
Date_Time_Measured = "07/27/11 074854"
It works correctly, however the Date_Time_Measured is always changing dates.
dim Date_Time_Measured as string
Dim myDT As DateTime = DateTime.ParseExact(Date_T
strdate = myDT.ToString("MM/dd/yyyy HH:mm:ss tt")
I still get the same error. If I add this line
Date_Time_Measured = "07/27/11 074854"
It works correctly, however the Date_Time_Measured is always changing dates.
you should use "try catch block"
are you using this in loop? how it is changing? you said there will be two different formats, and I put both in try...catch... block... see code below
are you using this in loop? how it is changing? you said there will be two different formats, and I put both in try...catch... block... see code below
dim Date_Time_Measured as string = "...."
Dim myDT As DateTime
Try
myDT = DateTime.ParseExact(Date_Time_Measured, "MM/dd/yy HHmmss", CultureInfo.InvariantCulture)
Catch ex As Exception
myDT = DateTime.ParseExact(Date_Time_Measured, "MM/dd/yy HH:mm:ss", CultureInfo.InvariantCulture)
End Try
strdate = myDT.ToString("MM/dd/yyyy HH:mm:ss tt")
ASKER
It still is giving me the original error about the string not being a valid DateTime
what code are you using now, and what kind of data do you have?
on which line do you get that error, exact error message please...
on which line do you get that error, exact error message please...
ASKER
If I use this code below, the problem file will get caught in the try catch and just assign whatever string is in the Date_Time)Measured variable to the strdate string variable.
Try
strdate = Date.Parse(Date_Time_Measu red).ToStr ing("yyyy MM dd HH:mm:ss")
Catch ex As Exception
strdate = Date_Time_Measured
End Try
Try
strdate = Date.Parse(Date_Time_Measu
Catch ex As Exception
strdate = Date_Time_Measured
End Try
strdate = Date.Parse(Date_Time_Measu red).ToStr ing("yyyy MM dd HH:mm:ss")
this says, convert Date_Time_Measured to date (but it does not say use this pattern) then convert it to string in "yyyy MM dd HH:mm:ss" format...
all the codes I posted are using a format to convert the string to datetime which you miss that point...
this says, convert Date_Time_Measured to date (but it does not say use this pattern) then convert it to string in "yyyy MM dd HH:mm:ss" format...
all the codes I posted are using a format to convert the string to datetime which you miss that point...
dim Date_Time_Measured as string = "...."
Dim myDT As DateTime
Dim strdate as string
Try
myDT = DateTime.ParseExact(Date_Time_Measured, "MM/dd/yy HHmmss", CultureInfo.InvariantCulture)
Catch ex As Exception
Try
myDT = DateTime.ParseExact(Date_Time_Measured, "MM/dd/yy HH:mm:ss", CultureInfo.InvariantCulture)
Catch ex As Exception
strdate = Date_Time_Measured
End Try
End Try
if strdate is not nothing then strdate = myDT.ToString("MM/dd/yyyy HH:mm:ss tt")
ASKER
This code that you provided me when implemented it says the ex hides a variable in an enclosing block
Dim myDT As DateTime
Try
myDT = DateTime.ParseExact(Date_Time_Measured, "MM/dd/yy HHmmss", CultureInfo.InvariantCulture)
Catch ex As Exception
Try
Catch ex As Exception
myDT = DateTime.ParseExact(Date_Time_Measured, "MM/dd/yy HH:mm:ss", CultureInfo.InvariantCulture)
strdate = Date_Time_Measured
End Try
End Try
instead of ex use e or x on line 6
ASKER
OK using the code: Dim temp As DateTime = DateTime.ParseExact(strdat e.Trim(), "MM/dd/yy hhmmss", System.Globalization.Cultu reInfo.Cur rentCultur e.DateTime Format)
That you provided me. It somehow doesn;t work when I get this string:" 11/10/11 130220" but when I get this string "07/27/11 074854" Iit works just fine.
Does it have something to do with the 24 hour clock?
That you provided me. It somehow doesn;t work when I get this string:" 11/10/11 130220" but when I get this string "07/27/11 074854" Iit works just fine.
Does it have something to do with the 24 hour clock?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
strdate = Date.Parse(Date_Time_Measu
use different format like
strdate = Date.Parse(Date_Time_Measu
strdate = Date.Parse(Date_Time_Measu