Assuming the filename will always contain the date embedded within it, but that it won’t be uniform, is there a way to parse such and consistently convert the filename into a DateTime?
More specifically, if the filename always has the same format, such as:
“NNNNN.YYYY-MM-DD.log”
…this is pretty easy.
But, if the filenames can be in any of the following formats:
“NNN.YYYY-MM-DD.log” or
“NNNNN.YYYY-MM-DD.txt” or
“NNNNNNN.YYYY-MM-DD.bunyan”
— and maybe even:
“NNNNN.MM-DD-YYYY.xlw”
-and:
“NNNNN.YYYY-DD-MM.zsj”
— IOW, the filenames always contain the date represented in some fashion, but otherwise, all bets are off, is there hope, or nope?
No, there’s no hope unless you can match an extension up to a known format.
Some days/month combinations you’ll get lucky like 2012-12-31. Clearly, 31 isn’t the month value and you can deduce at least the day/month/4-digit year combo.
As Alexei mentioned, a 2 digit year further complicates the problem.
As a side note, this is pretty similar to a SQL Server problem. This is fine and dandy until it’s run with different date time format expectations:
SELECT CAST('2012-05-01' AS DATETIME)Depending on the locale, this could be May 1, 2012 or January 5, 2012.
(See this thread: Error converting string to datetime due to locale)