views:

211

answers:

2

I have a string which represents a DataTime value, and I want to workout what string format was used to create the string.

For example
- Given "Wednesday 27 Jan 2010" I expect "dddd dd MMM yyyy"
- Given "2010 01 27" I expect "yyyy MM dd"

Assume that the date is close to DateTime.Now and relates to the CurrentCulture. So given that we have en-GB culture
- Given "01 01 2010" I expect "dd MM yyyy"

Is there a simple way to do this?

+1  A: 

The simplest thing to do is look at the reference for datetime formatting strings and working them out.

You may be able to use reflection in order to get this, or use disassembler (such as ILDASM) to find all the strings used in an assembly and guess at which ones are datetime formatting string.

If disassmbling, you can search for the days of the week and for a string starting with dddd, which should tell you how the original was constructed.

Oded
A: 

you could split on whitespace to get an array of strings then you could test the elements in the array against known values to try to guess the format

e.g. if you search for "Wednesday" if you find it assume "dddd"

if a four digit number assume yyyy

days and months could be a problem, you could test for > 12 for the days but that's pretty bad

maybe you could infer the format using whitespace split then DateTime.Parse on the orignal and test against a formatted (using the infered format) version of the parsed date for equality

PeanutPower
With the current culture, I can check the names of the Days of week somehow?
Sophie88
you could make an assumption about the order of two digit days and months based on the current culture but the "old software" might not be respecting the current culture anyway
PeanutPower
@Sophie88: yes, you can use the DateTimeFormat property of the CultureInfo class which has itself a property called DayNames which returns a list of the day names for that culture.
tomlog
I'll start working on this now, thanks.
Sophie88