Link to home
Start Free TrialLog in
Avatar of Starr Duskk
Starr DuskkFlag for United States of America

asked on

String was not recognized as a valid DateTime.

I have this date string:
2018/05/23 22:15:14

I want to subtract 5 hours and return the date time in AM/PM format. But when I do, I get the message:
String was not recognized as a valid DateTime.

I've tried:

                  Dim newdate As DateTime = Now()
                    Dim getDate2 As String = "2018/05/23 22:15:14"

 newdate = DateTime.ParseExact(getDate2, "MM/dd/yyyy HH:mm:ss", Nothing)
and this:
newdate = DateTime.Parse(getDate2)

I was going to do this afterwards to deduct the 5 hours, but I never get that far:
newdate = newdate.Date().AddHours(-5)

I saw a webpage with an example and it has this:
' From w3.org
        time = DateTime.Parse("2009/02/26 18:37:58")

So why is this not a valid datetime?
2018/05/23 22:15:14

thanks!
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore 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
SOLUTION
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
SOLUTION
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
Avatar of Starr Duskk

ASKER

they all worked as required.

The last one with the -5 hours has me confused however, because i'm not sending it to the console. I want to save it to a variable:
>>Console.WriteLine($"{newdate.AddHours(-5):MM/dd/yyyy hh:mm:ss tt}")

So I wasn't able to use that part of this one to see how that part worked.

Thanks!
You can always subtract the hours like Gustav demonstrated above:
Module Module1
    Sub Main()
        Dim newdate As DateTime = Now
        Dim getDate2 As String = "2018/05/23 22:15:14"

        newdate = DateTime.ParseExact(getDate2, "yyyy/MM/dd HH:mm:ss", Nothing).AddHours(-5)
        Console.WriteLine($"{newdate:MM/dd/yyyy hh:mm:ss tt}")
        Console.ReadLine()
    End Sub
End Module

Open in new window

Will give the same output as above.  In case the formatting of the string is confusing you:
' This
Console.WriteLine($"{newdate:MM/dd/yyyy hh:mm:ss tt}")
' Is the same as this
Console.WriteLine("{0:MM/dd/yyyy hh:mm:ss tt}", newdate)
' Which is the same as this
Console.WriteLine(newdate.ToString(MM/dd/yyyy hh:mm:ss tt))

Open in new window

Proof of concept -
Module Module1
    Sub Main()
        Dim newdate As DateTime = Now
        Dim getDate2 As String = "2018/05/23 22:15:14"

        newdate = DateTime.ParseExact(getDate2, "yyyy/MM/dd HH:mm:ss", Nothing).AddHours(-5)
        Console.WriteLine($"{newdate:MM/dd/yyyy hh:mm:ss tt}")
        Console.WriteLine("{0:MM/dd/yyyy hh:mm:ss tt}", newdate)
        Console.WriteLine(newdate.ToString("MM/dd/yyyy hh:mm:ss tt"))
        Console.ReadLine()
    End Sub
End Module

Open in new window

Which produces the following output -User generated image-saige-
This will only work as expected in the US, as tt is localised and / is not a slash but the date separator.
Output here is:

05-23-2018 05:15:14 
05-23-2018 05:15:14 
05-23-2018 05:15:14 

Open in new window