Question DateTime issues

Lorival

New member
Joined
Feb 1, 2011
Messages
3
Programming Experience
Beginner
Hi guys,
I have been struggling with this problem for several hours now and can't find the reason for the error message...
I am reading a datetime string from a txt file, it returns this:
"01/31/2011 6:13 AM"
Now, I want to convert this string to datetime format. I don’t want to change the structure of the date... just want to convert in order to do some minutes and seconds calculations.

I have tried all kind of options:
Timespan
Datediff
Parse
ParseExact with format.... culture... globalization
Nothing seems to work!

Maybe I’m going crazy here....
I truly appreciate all your help.
Thanks.
 
Not sure why you're having problems. This gets me #1/31/2011 6:13:00 AM#

VB.NET:
        Dim dateStr As String = "01/31/2011 6:13 AM"
        Dim theDate As DateTime = DateTime.Parse(dateStr)
 
Thanks MattP,

I did a copy-past of your code and i get:
"String was not recognized has a valide DateTime."

If I change the date to "31/01/2011 6:13 AM" then it works!!!
It converts to "1/31/2011 6:13 AM" but i don't want that... i want the same way as the original!!!
 
Here's an example using DateTime.ParseExact and DateTime.TryParseExact.

VB.NET:
        Dim dateStr As String = "01/31/2011 6:13 AM"
        Dim formats As String() = {"MM/dd/yyyy hh:mm tt", "M/dd/yyyy hh:mm tt", "MM/dd/yyyy h:mm tt", "M/dd/yyyy h:mm tt"}

        Dim theDate As DateTime
        DateTime.TryParseExact(dateStr, formats, Nothing, DateTimeStyles.None, theDate)

        Dim theDate2 As DateTime = DateTime.ParseExact(dateStr, formats, CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces)
 
Thanks MattP,

That worked!!
I believe my problem was the format, I wasn't including all the options.

Thank you again.!
 
Back
Top