1

First off, I realize there's a million pages discussing this already. I have looked at least a hundred of them but cannot seem to make this work. My date and time is presented as a string, compiled from javascript to grab client's local time. It is formatted like this: 7/11/2015 8:34 PM. I currently have:

Dim datetimeformated = DateTime.ParseExact(lblDateTime.Text, "MM/dd/yyyy HH:mm tt", CultureInfo.InvariantCulture)

I have tried many different variants, but this should be correct I think, yet it does not work. Any help is greatly appreciated. TIA

1 Answer 1

3

The correct format for your case is: M/dd/yyyy h:mm tt, and perhaps even, M/d/yyyy h:mm tt, if you can have the day of the month as a single digit.


Explanation: Why your format string didn't work.

MM: means that you must always have 2 digits for the month, clearly not the case in your example.

dd: again, means that you must always have 2 digits for the day of the month. Is that the case? Adjust the parameter if needed.

HH: This actually means that you are expecting the hour value as 2-digits using the 24-hour clock (00-23), which is clearly wrong on both accounts. You can have a single digit, and you are not using the 24-hour clock, because you are using the AM/PM designator.

Relevant documentation link: Custom Date and Time Format Strings.

Sign up to request clarification or add additional context in comments.

8 Comments

Thank you, I do appreciate your explanations. However this is still throwing the same error. I am now using "M/d/yyyy h:mm tt". Could the issue be with the CultureInfo argument?
No. If anything, using CultureInfo.InvariantCulture ensures consistent behavior, so it should actually make it easier for us to see the same results. So this doesn't work for you? Console.WriteLine(DateTime.ParseExact("7/11/2015 8:34 PM", "M/d/yyyy h:mm tt", CultureInfo.InvariantCulture));. Or are you getting errors with a different date string?
Dim datetimeformated = DateTime.ParseExact("7/11/2015 8:34 PM", "M/d/yyyy h:mm tt", CultureInfo.InvariantCulture) works just fine. But when I substitute my string it throws the error.
If I was debugging this, this is what I would do: Add this line to the code: System.Diagnostics.Debug.WriteLine("'" & lblDateTime.Text & "'"). Then I would run the code with the DebugView tool open to see the line. Then I would examine the string closely to see if there is something odd about it. If you can post the string that you get, that would be helpful. Again, there must be something funny and unexpected about the string, even if it seems to display alright. You need to find what that is. An extra space somewhere?
Well, at least that explains why the formatting didn't work. Now you need to figure out why it's blank :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.