views:

310

answers:

6

I want to parse the date from the string where date formate can be any of different format.

Now to match date we can use DateTime.TryParseExact and we can define format as we needed and date will be matched for any different format.

string[] formats = {"MMM dd yyyy"};

            DateTime dateValue;
            string dateString = "May 26 2008";

            if (DateTime.TryParseExact(dateString, formats,
                                           new CultureInfo("en-US"),
                                           DateTimeStyles.None,
                                           out dateValue))

                    MessageBox.Show(dateValue.ToString());

This matches with date.But this is not working for parse the date from the string that is it does not matched with the date which is in some string.

Like if the date is "May 26 2008" then we can define format "MMM dd yyyy" and date will be matched.

But if date is in some string like "Abc May 26 2008" then date will not be matched.So for that can we use regular expression here ? If yes how ?

The string from I want to parse the date, is parsed from the html page and the string can be any different.

EDIT : I want to write the format like which matches any string in which there is a date using regex.

+1  A: 

If it's English only and the format is "MMM dd yyyy" you can search where your string is [January|February|...|December] day year.

But you should first ask yourself why you're parsing any string. Can you not force the user to use a predefined format and validate that input?

Carra
@Carra,It's not like that.I am parsing html page for displaying some useful information in the datagridview.For that I will check every table in the html page,and in every table I check I every row,and for every I check every columninnertext that is there date in the columninnertext and columninnertext can be any string.
Harikrishna
A: 

If you know your date will start with a month then you can use substring to get that part. (Find occurence of Jan/Feb/ etc)

PoweRoy
+2  A: 

You could do a regular expression match on something like @"[A-Za-z]{3} \d{2} \d{4}" and feed whatever matches into DateTime.TryParseExact. It might break for alternate cultures however, I'm not sure if there are languages around that have month names only 2 letters short or something :)

Alternatively, you could extract the month names from cultureInfo.DateTimeFormat.AbbreviatedMonthNames and use that to build a slightly better targeted regular expression. It should also work for other cultures.

Edit - here's an example:

string text = "Apr 03 2010 foo May 27 2008 bar";
CultureInfo ci = new CultureInfo("en-US");
Regex regex = new Regex(@"(?<date>(" + String.Join("|",
    ci.DateTimeFormat.AbbreviatedMonthNames, 0, 12) + @") \d{2} \d{4})");

// Builds this regex:
// (?<date>(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \d{2} \d{4})

var matches = regex.Matches(text);
foreach (Match match in matches)
{
    string capturedText = match.Groups["date"].Value;
    DateTime dt;
    if (DateTime.TryParseExact(capturedText, "MMM dd yyyy", ci,
        DateTimeStyles.None, out dt))
    {
        Console.WriteLine(capturedText + ": " + dt.ToLongDateString());
    }
}

// Prints two parsed dates in long format
Thorarin
@Thorarin,I don't understand.
Harikrishna
@Harikrishna: here's a working example. Modified the code slightly, because apparently the month array contains 13 elements. Example will only work with Gregorian calendar.
Thorarin
@Thorarin,it does not work for `Apr032010`.
Harikrishna
@Harikrishna: I'm not sure what you're looking for, but that input doesn't follow the pattern you supplied in your `TryParseExact` statement. Thus, it is not designed to work with that type of input.
Thorarin
@Thorarin, I want to define more than one format for different date to match more than one format of date.
Harikrishna
@Harikrishna: You can expand the regular expression to include those formats as well, but I cannot guess which formats you're interested in. There is no (sane) solution that will work on all the formats at once, so you'll just have to read up on regular expression syntax :)`TryParseExact` allows you to provide an array of recognized formats, or you could use `DateTime.TryParse` instead.
Thorarin
A: 

I think something like \w{3,8} \d\d \d\d\d\d[\s$] would work most of the time if it's in US format, but I wouldn't trust it too much if the text you're parsing could be just anything.

ho1
A: 

You can customize the format according to your needs:

private const string DateTimeFormat = "dd-MMM-yy hh.mm.ss.ffffff tt"; 

public static bool TryParseToDateTime(this string stringValue, out DateTime result)
{
    if (String.IsNullOrEmpty(stringValue))
    {
        result = DateTime.MinValue;
        return false;
    }

    return DateTime.TryParseExact(stringValue, DateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out result);
}

UPDATE: You probably should use regular expressions to find strings that match date in text. You have to decide what date format you expect and write (or choose) an appropriate regular expression. For example, for "dd MMM yyyy" format you can use the following regular expressions:

^\d{2}\s{1}(Jan|Feb|Mar|Apr|May|Jun|Jul|Apr|Sep|Oct|Nov|Dec)\s{1}\d{4}$

by Stephen Lam from http://regexlib.com/REDetails.aspx?regexp_id=325

Alternatively you can browse this site to find appropriate expression.

Boris Modylevsky
@Boris,String is not fixed it can be any type of string.
Harikrishna
Harikrishna, how would you like to parse "1/3/10"? It might be 1 March 2010 or 3 January 2010 or anything else? You have to define desired format and use regular expression to find matches in text
Boris Modylevsky
@Boris,Only one format it is 01/03/2010.But if date is in any of string it should be matched.
Harikrishna
@Boris,We can not make a regex that matches any type of string with date while writing format for tryparse method.
Harikrishna
@Harikrishna, I am sorry, but "01/03/2010" is not a format. Format is "dd/MM/yyyy" or "MM/dd/yyyy". I am not sure I understand correctly your requirements. You said that you need one format supported, but you commented above that you "want to define more than one format". Could you please clarify what your requirements are?
Boris Modylevsky
@Boris,any format of date should be matched which we define in the format parameter of tryparse method.But if dates specified in the format matches,but if it is any string it should also be matched.
Harikrishna
A: 

Here is the link to parse the date from the string which is very good.There is set of regex to parse the date from the string.

http://www.codeproject.com/KB/datetime/date_time_parser_cs.aspx

Harikrishna