Link to home
Start Free TrialLog in
Avatar of cmdolcet
cmdolcetFlag for United States of America

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_Measured).ToString("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
Avatar of HainKurt
HainKurt
Flag of Canada image

am trying to parse the string: 07/27/11 074854 using the following:
strdate = Date.Parse(Date_Time_Measured).ToString("yyyy MM dd HH:mm:ss")

use different format like

strdate = Date.Parse(Date_Time_Measured).ToString("MM/dd/yy HHmmss")
strdate = Date.Parse(Date_Time_Measured).ToString("MM/dd/yyyy HHmmss")

try this:

Try
  strdate = Date.Parse(Date_Time_Measured).ToString("MM/dd/yy HHmmss")
Catch ex As Exception
  strdate = Date.Parse(Date_Time_Measured).ToString("MM/dd/yy HH:mm:ss")
End Try

Avatar of cmdolcet

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.InvariantCulture)

and then use

dt.ToString("the format you like here")

for example

now.toString("MM/dd/YYYY HH:mm:ss tt")
OK what is myds and cultureInfo both are returning undeclaired
Avatar of Nasir Razzaq
myds is the date string and use

System.Globalization.CultureInfo.InvariantCulture

instead of

CultureInfo.InvariantCulture
I get a FormatException was unhandled with the following code:

  Dim dt As DateTime
            dt = DateTime.ParseExact(Date_Time_Measured, "MM/dd/yy HHmmss", System.Globalization.CultureInfo.InvariantCulture)
            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?
No I get the error on the following line:
  dt = DateTime.ParseExact(Date_Time_Measured, "MM/dd/yy HHmmss", System.Globalization.CultureInfo.InvariantCulture)
did you add this to your page

Imports System.Globalization
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.InvariantCulture)
response.write(now.toString("MM/dd/YYYY HH:mm:ss tt"))
oops, it should be yyyy

dim myDS as string = "07/27/11 074854"
now = DateTime.ParseExact(myds,"MM/dd/yy HHmmss",CultureInfo.InvariantCulture)
response.write(now.toString("MM/dd/yyyy HH:mm:ss tt"))

and result is

07/27/2011 07:48:54 AM
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.InvariantCulture)
response.write(myDT.toString("MM/dd/yyyy HH:mm:ss tt"))
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_Time_Measured, "MM/dd/yy HHmmss", CultureInfo.InvariantCulture)
            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
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")

Open in new window

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...
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_Measured).ToString("yyyy MM dd HH:mm:ss")
            Catch ex As Exception
                strdate = Date_Time_Measured
            End Try
strdate = Date.Parse(Date_Time_Measured).ToString("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...
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")

Open in new window

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

Open in new window

instead of ex use e or x on line 6
OK using the code: Dim temp As DateTime = DateTime.ParseExact(strdate.Trim(), "MM/dd/yy hhmmss", System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat)

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
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial