Click here to Skip to main content
15,939,853 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So I need to parse through a date of a filename. So far example I have a file and the date is
The problem I have at the moment is the format of the date is something like "M/dd/yyyy" and so every time I use ParseExact,
I get an error that the string is not a valid DateTime, even if I do "MM/dd/yyyy". What are my other options to pick this date out?

The above date reads as: February 1st 2017

What I have tried:

string temp = filename.Replace(".txt", "").Substring(filename.LastIndexOf('_') + 1);
           DateTime extracted = DateTime.ParseExact(temp, "Mddyyyy", System.Globalization.CultureInfo.InvariantCulture);
Updated 2-Feb-17 17:18pm
Dave Kreskowiak 2-Feb-17 12:49pm    
A quick solution would be to get the date string, check it's length, then switch to an appropriate parse string, one for one digit months and one for two digit months.

Really, your data is what's the problem. In a normalized format, you would have a consistent number of digits for all month , day, and year numbers. This means leading zeros on values to fill the required number of digits to maintain consistency in the data.
SmartDeveloping 2-Feb-17 12:50pm    
Dave, unfortunately that is the sad part that it isn't in a normalized format or I wouldn't even have this problem.
[no name] 2-Feb-17 13:20pm    
Use an If statement to check the length of temp and add a "0" to the front of the string if the length is not 8. If whomever is giving you these filename is not going to noramlize them for you, you are going to have to do it yourself.
[no name] 2-Feb-17 13:31pm    
You didn't read the comment very well did you?
SmartDeveloping 2-Feb-17 13:41pm    
Sorry, I understand what you mean.

try this
it will work for "MMddyyyy" and "Mddyyyy"

string filename = "Accounts_2012017.txt";
           string temp = filename.Replace(".txt", "").Substring(filename.LastIndexOf('_') + 1);
           string format = (temp.Length == 7) ? "Mddyyyy" : "MMddyyyy";
           DateTime extracted = DateTime.ParseExact(temp, format, System.Globalization.CultureInfo.InvariantCulture);
Share this answer
SmartDeveloping 3-Feb-17 15:19pm    
Thanks a lot, I was able to make it work with something similar.
Karthik_Mahalingam 3-Feb-17 15:20pm    
According to the example filename you posted, your format string is incorrect. It should be "yyyyMdd". BTW, if it's not too late to change it, it would be better if you used a two-digit month. That way, there's no confusion about what the date is, and your parsing will always work.

I don't personally think you'll be able to reliable parse the date because you're allowing the month to be represented by a single digit. To mitigate that aspect of the filename, you could check the length of the string that represents the date, and if it's less than 8, prepend it with a zero. At that point, you could change your format string to "MMddyyyy", and it will parse correctly every time (assuming, of course that the day is always represented by two digits).

FWIW, you should also consider breaking that statement up into discrete parts so you can correct the date (and handle errors) more easily.
Share this answer
SmartDeveloping 2-Feb-17 12:45pm    
Error:String was not recognized as a valid DateTime. Not sure how the "yyyyMdd" would help because 2017 is at the end of it. The date reads February 1st 2017.
#realJSOP 2-Feb-17 13:14pm    
Updated my answer.
SmartDeveloping 2-Feb-17 13:17pm    
Unfortunately I cannot change the code of how the file is padded, because I am getting it from third party. I am checking the length, lets see if I can prepend it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900