String was not recognized as a valid DateTime

cmdolcet
cmdolcet used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
HainKurtSr. System Analyst

Commented:
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")

HainKurtSr. System Analyst

Commented:
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

Author

Commented:
No this gave me an error message String was not recognized as a valid DateTime
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

HainKurtSr. System Analyst

Commented:
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")

Author

Commented:
OK what is myds and cultureInfo both are returning undeclaired
Most Valuable Expert 2012
Top Expert 2014

Commented:
myds is the date string and use

System.Globalization.CultureInfo.InvariantCulture

instead of

CultureInfo.InvariantCulture

Author

Commented:
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")
Most Valuable Expert 2012
Top Expert 2014

Commented:
>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?

Author

Commented:
No I get the error on the following line:
  dt = DateTime.ParseExact(Date_Time_Measured, "MM/dd/yy HHmmss", System.Globalization.CultureInfo.InvariantCulture)
HainKurtSr. System Analyst

Commented:
did you add this to your page

Imports System.Globalization

Author

Commented:
Yes. I dont understand why the format "07/25/11 074854" doesn;t get recognized.
HainKurtSr. System Analyst

Commented:
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"))
HainKurtSr. System Analyst

Commented:
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

Author

Commented:
When I use your code I get an error property NOW is ReadOnly ?????
HainKurtSr. System Analyst

Commented:
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"))

Author

Commented:
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.
HainKurtSr. System Analyst

Commented:
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

Author

Commented:
It still is giving me the original error about the string not being a valid DateTime
HainKurtSr. System Analyst

Commented:
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...

Author

Commented:
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
HainKurtSr. System Analyst

Commented:
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

Author

Commented:
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

HainKurtSr. System Analyst

Commented:
instead of ex use e or x on line 6

Author

Commented:
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?
Sr. System Analyst
Commented:
try this (from your other open question)

see question @ 27259290 for more detail
dim dt as datetime = Nothing
dim myInStr as string = "07/27/11 154854"
dim myOutStr as string = ""

Try
	dt = DateTime.ParseExact(myInStr,"MM/dd/yy HHmmss",CultureInfo.InvariantCulture)
Catch
	Try
		dt = DateTime.ParseExact(myInStr,"MM/dd/yy HH:mm:ss",CultureInfo.InvariantCulture)
	Catch
		' still have issue, use original
		myOutStr = myInStr		
	End Try
End Try

if (not dt = nothing) then myOutStr = dt.toString("MM/dd/yyyy HH:mm:ss tt")
response.write(myOutStr)

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial