Harikrishna
Harikrishna

Reputation: 4305

How to parse date from string?

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.

Upvotes: 1

Views: 3345

Answers (6)

Harikrishna
Harikrishna

Reputation: 4305

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

Upvotes: 0

Boris Modylevsky
Boris Modylevsky

Reputation: 3099

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.

Upvotes: 1

Thorarin
Thorarin

Reputation: 48476

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

Upvotes: 3

Hans Olsson
Hans Olsson

Reputation: 55001

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.

Upvotes: 0

Carra
Carra

Reputation: 17964

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?

Upvotes: 1

RvdK
RvdK

Reputation: 19790

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)

Upvotes: 0

Related Questions