0

I am fairly new to C#.

I have to build a script that reads a string and tries to convert it to a Date.

The problem are:

-The dates come in different formats. (International, American, Japanese)

-The dates come with different separators, or none at all.

-The year might be complete or just 2 positions

I have begun by replacing the possible separators:

dateinstring = dateinstring.Replace("-","");
dateinstring  = dateinstring.Replace(".","");
dateinstring = dateinstring.Replace("/","");

Then check the lenght to see if the year is complete (31102012 or 311012 for example)

if(dateinstring.lenght = 8){
//check format with complete date
}else{
//check format with reduced year
}

Now I stumbled upen this function: DateTime.TryParseExact which seems to do the same, but it returns a boolean (true or false) and not the date itself.

Am I missing something? Are there methods in C# that already do this or should I go on with my solution?

prgrm
  • 3,734
  • 14
  • 40
  • 80
  • 2
    There is an `out` argument - [`out DateTime result`](https://msdn.microsoft.com/en-us/library/ms131044(v=vs.110).aspx) – Wiktor Stribiżew Sep 07 '16 at 07:49
  • 1
    The method `TryParseExact` has an `out` parameter defined which is set in the method. Take a look at the example on the MSDN Page https://msdn.microsoft.com/en-us/library/h9b85w22(v=vs.110).aspx – Bojan B Sep 07 '16 at 07:49
  • 1
    What is "International" date format? ISO standard? (Which is the same as Japanese, if you just have the date part) – Matthew Watson Sep 07 '16 at 07:49
  • 1
    See http://stackoverflow.com/questions/5366285/parse-string-to-datetime-in-c-sharp – Wiktor Stribiżew Sep 07 '16 at 07:51
  • 2
    Without knowing datetime culture it might lead to some interesting misunderstandings - UK(dd/MM/yyyy) and USA(MM/dd/yyyy). – Vytautas Plečkaitis Sep 07 '16 at 07:52
  • 1
    I do not think above is the good way (i.e. convert string and etc..), instead of that, how about using iFormatProvider? [link](https://msdn.microsoft.com/library/cc165448.aspx) – Sean83 Sep 07 '16 at 07:53
  • In order to unambiguously parse these dates you will need to exclude the DDMMYYYY date format (Fortunately, I don't see that in your list of date formats). Furthermore you will need to limit the range of valid years, otherwise a date such as "12011010" will be ambiguous: In yyyymmdd format that would be 1201-10-10, but if parsed in mmddyyyy format that would be 1010-12-01 – Matthew Watson Sep 07 '16 at 09:03

1 Answers1

1

Here is a function that I use in a project when the built-in parsing fails - so it only covers additional formats, not the standard ones.

private DateTime? TryParseNumberString(string dateText)
{
    // additional date format options to be accepted
    var extraDateFormats = new[]
    {
        "d.M.",
        "d.M",
        "ddMMyyyy",
        "ddMMyy",
        "ddM"
    };
    foreach (var item in extraDateFormats)
    {
        DateTime test;
        if (DateTime.TryParseExact(dateText, item, null, System.Globalization.DateTimeStyles.AssumeLocal, out test))
        {
            return test;
        }
    }
    return null;
}

The approach was basically, that I thought of some additional date representations that I want to support and the order of evaluation so that ambiguous strings are evaluated according to my wishes.

Depending on your requirements you may alter the format string array and maybe add a specific culture in case you want to support long date formats with written day / month.

You can also try DateTime.TryParse(input, culture, DateTimeStyles.None, out result) with different culture values, in order to try-parse english, japanese and others by convention.

grek40
  • 13,113
  • 1
  • 24
  • 50
  • Why not use [the overload of `DateTime.TryParseExact()`](https://msdn.microsoft.com/en-us/library/h9b85w22(v=vs.110).aspx) which lets you pass an array of date formats to try? – Matthew Watson Sep 07 '16 at 14:27
  • @MatthewWatson practically, your remark is probably spot on, but I can't find any piece of documentation right now that guarantees the order of evaluation, if multiple formats match the same string. – grek40 Sep 07 '16 at 15:11
  • Yeah, the documentation isn't very explicit about this, but if you check the source code on referencesource you can see that it loops in the obvious way (i.e. in order of the items in the array) – Matthew Watson Sep 08 '16 at 06:25