DateTime.ParseExact issues

jimctr

Well-known member
Joined
Dec 1, 2011
Messages
52
Programming Experience
Beginner
I am reading Exif data and my DateTime is retrieved and converted to string from ascii and is of the form "2011:05:11 21:28:56" or with the format "yyyy:MM:dd HH:mm:ss". The relevant code snippet is posted below. VB.Net 2010 complains that the "String was not recognized as a valid DateTime."

'Set up for conversion of datetime
Dim provider As CultureInfo = CultureInfo.InvariantCulture
Dim format As String = "yyyy:MM:dd HH:mm:ss"
Dim prop As String

prop = encoding.GetString(propItems(count).Value) 'Run the Ascii Encoding
Dim date1 As DateTime = DateTime.ParseExact(prop, format, provider) 'Converts from string to DateTime


Any clue as to what I might be doing wrong? Thanks
 
OK modified the main string returned by Exif

Er yeah, because the date string doesn't match it. Has to be 2011:05:11:21:28:56 AND yyyy:MM:dd:HH:mm:ss

Dim MyString As String 'This is used to process date-time. Strings cannot have any blank spaces
Dim format As String = "yyyy:MM:dd:HH:mm:ss"

MyString = prop.Replace(" ", ":") ' Now the prop appears as 2011:05:11:21:28:56 eliminating the empty space and appearing in the same format 'as format

Dim date1 As DateTime = DateTime.ParseExact(MyString, format, provider) 'Converts from string to DateTime

However still generates an error. Any possible issue with provider?

Dim provider As CultureInfo = CultureInfo.InvariantCulture

Thanks
 
Since ParseExact is not really working for me, this is my work around (since I have to change the EXIF string in any case)

MyString = prop.Replace(" ", ":")
Dim MyArray As String() = MyString.Split(":")
Dim MyDateTime = New DateTime(MyArray(0), MyArray(1), MyArray(2), MyArray(3), MyArray(4), MyArray(4))

This way I don't even need to parse, since I can simply invoke MyDateTime.Month for example. The only downside to this is that it converts my 24 hour clock to a 12 hour clock AM/PM and the conversion of seconds is flawed, but I don't really use seconds in any case
 
Dim MyString As String 'This is used to process date-time. Strings cannot have any blank spaces
Dim format As String = "yyyy:MM:dd:HH:mm:ss"

MyString = prop.Replace(" ", ":") ' Now the prop appears as 2011:05:11:21:28:56 eliminating the empty space and appearing in the same format 'as format

Dim date1 As DateTime = DateTime.ParseExact(MyString, format, provider) 'Converts from string to DateTime

However still generates an error. Any possible issue with provider?

Dim provider As CultureInfo = CultureInfo.InvariantCulture

Thanks

Well I don't know why you bothered to put it in a variable but it works fine for me ...

Dim d As String = "2011:05:11:21:28:56"
Dim dt = DateTime.ParseExact(d, "yyyy:MM:dd:HH:mm:ss", CultureInfo.InvariantCulture)
 
Your format string is wrong Dun, his original date format has a space between the date and the time. And the space works just fine.. In a custom date format, any non-formatting characters are treated as literals (this is the opposite of a standard date format string behavior), and since " " is not a formatting character it is just copied to the output string. I didn't use any provider for my test...

Dim d As String = "2011:05:11 21:28:56"
Dim dt = DateTime.ParseExact(d, "yyyy:MM:dd HH:mm:ss", Nothing)

MessageBox.Show(dt.ToString())
 
Last edited:
Define 'wrong'! It works just as well and it solved the error which I was also getting using the original code. But I say 'was' because now I'm not which either means that some sneaky editing's been done overnight or my computer and the OP's are in cahoots to annoy the hell out of us. Go figure!
 
Back
Top