0
DateTime date = new DateTime(1970, 1, 15);
string sdate = date.ToString(value_i_will_forget as string,
    CultureInfo.InvariantCulture);

// some lines later

string format_string = MagicParse(sdate,
    CultureInfo.InvariantCulture);

I have an arbitrary date that I write to a string with CulturInfo.InvariantCulture. The .Net format I use - "d", for example - is now forgotten.

I do know however that the string hasn't changed, its still valid and its still with invariant culture. Im looking for the MagicParse function to extract the format string used to write the formatted date.

Mizipzor
  • 51,151
  • 22
  • 97
  • 138
  • Are you only allowing standard format strings, or are custom format strings allowed as well? – Greg Feb 17 '10 at 16:37
  • 1
    Only standard format strings. Actually, even just a subset of the standard formats. Are you suggesting a brute force approach? – Mizipzor Feb 17 '10 at 16:42
  • Sort of. I don't know of any built-in methods to do this. – Greg Feb 17 '10 at 16:51

2 Answers2

0

Unless you have a sample of dates in which the day exceeds 12, then there is no feasible way that this would ever be safe.

spender
  • 117,338
  • 33
  • 229
  • 351
  • Even though we are able to assume that the format haven't changed? – Mizipzor Feb 17 '10 at 16:33
  • 2
    Even then you're not certain. A 13 can be either the day or 2013. – Carra Feb 17 '10 at 16:35
  • 5
    Example: The date is January 1st, 2001. The format strings "MM/dd/yy", "dd/MM/yy", "yy/MM/dd"... all create "01/01/01". How do you reverse engineer that? – Greg Feb 17 '10 at 16:39
0

DateTime.Parse(your_date_string, CultureInfo.InvariantInfo)
, also you can add specific DateTimeStyles in third parameter
Try this one
http://msdn.microsoft.com/en-US/library/system.globalization.datetimeformatinfo.shortdatepattern.aspx

zabulus
  • 2,373
  • 3
  • 15
  • 27