Starr Duskk
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(getDat e2, "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!
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(getDat
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
So why is this not a valid datetime?
2018/05/23 22:15:14
thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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))
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
Which produces the following output --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:
Output here is:
05-23-2018 05:15:14
05-23-2018 05:15:14
05-23-2018 05:15:14
ASKER
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($"{new
So I wasn't able to use that part of this one to see how that part worked.
Thanks!