Trouble parsing dates

Arokh

Centurion
Joined
Apr 11, 2006
Messages
124
Hi,
I need to parse a specific date format, as fast as possible,
since there are around a million of them I need to parse in a go.

The date format looks like this:
"7/30 17:15:39.140"
"7/30 10:07:03.156"

After searching for an example on the MSDN I found this:
http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

[CSharp]
string dateString, format;
DateTime result;
CultureInfo provider = CultureInfo.InvariantCulture;

// Parse date and time with custom specifier.
dateString = "Sun 15 Jun 2008 8:30 AM -06:00";
format = "ddd dd MMM yyyy h:mm tt zzz";
try {
result = DateTime.ParseExact(dateString, format, provider);
Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
} catch(FormatException) {
Console.WriteLine("{0} is not in the correct format.", dateString);
}

[/CSharp]

So I tried to adapt to my needs but don't seem to get it right.
I always get an 'System.FormatException'

Here is what I tried:
[CSharp]
string dateString, format;
DateTime result;
CultureInfo provider = CultureInfo.InvariantCulture;

// Parse date and time with custom specifier.
dateString = "2008/7/30 13:52:15.765";
format = "yyyy/MM/DD HH:mm:ss.FFF";
try {
result = DateTime.ParseExact(dateString, format, provider);
Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
} catch(FormatException) {
Console.WriteLine("{0} is not in the correct format.", dateString);
}
[/CSharp]

On another note.
Is this faster than:
[CSharp]
string[] TimeStampParts = logEntry.Split(new Char[] { '/', ' ', ':', '.' });
TimeStamp = new DateTime(
(long.Parse(TimeStampParts[5]) +
(long.Parse(TimeStampParts[4]) +
(long.Parse(TimeStampParts[3]) +
(long.Parse(TimeStampParts[2]) +
(long.Parse(TimeStampParts[1]) +
long.Parse(TimeStampParts[0]) * 30) * 24) * 60) * 60) * 1000) * 10000);[/CSharp]
?

If not, are there any methods/tricks I can use to make it faster?
 
A format of "yyyy/MM/dd HH:mm:ss.fff" seems to work fine though, not looked into the difference between fff and FFF though so you may want to see if this is suitable.
 
Back
Top