Avatar of cmdolcet
cmdolcet
Flag 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
Visual Basic.NET

Avatar of undefined
Last Comment
HainKurt

8/22/2022 - Mon
HainKurt

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

HainKurt

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

cmdolcet

ASKER
No this gave me an error message String was not recognized as a valid DateTime
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
HainKurt

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")
cmdolcet

ASKER
OK what is myds and cultureInfo both are returning undeclaired
Nasir Razzaq

myds is the date string and use

System.Globalization.CultureInfo.InvariantCulture

instead of

CultureInfo.InvariantCulture
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
cmdolcet

ASKER
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")
Nasir Razzaq

>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?
cmdolcet

ASKER
No I get the error on the following line:
  dt = DateTime.ParseExact(Date_Time_Measured, "MM/dd/yy HHmmss", System.Globalization.CultureInfo.InvariantCulture)
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
HainKurt

did you add this to your page

Imports System.Globalization
cmdolcet

ASKER
Yes. I dont understand why the format "07/25/11 074854" doesn;t get recognized.
HainKurt

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"))
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
HainKurt

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
cmdolcet

ASKER
When I use your code I get an error property NOW is ReadOnly ?????
HainKurt

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"))
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
cmdolcet

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_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.
HainKurt

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

cmdolcet

ASKER
It still is giving me the original error about the string not being a valid DateTime
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
HainKurt

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...
cmdolcet

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

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

Your help has saved me hundreds of hours of internet surfing.
fblack61
cmdolcet

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

Open in new window

HainKurt

instead of ex use e or x on line 6
cmdolcet

ASKER
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?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
HainKurt

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question