Miller
Miller

Reputation: 1156

Extract Date time from the Sentence in c#

I am stuck in the extracting the datetime from the sentence or format.

Posted on Thursday, May 1st, 2014 at 10:07 AM

2014-06-20T02:11:06+00:00

Is there any good regex or tool in C# where i can find out datetime from these formats.

Upvotes: 1

Views: 1575

Answers (3)

Mike Hixson
Mike Hixson

Reputation: 5189

Here is a way you can do it simply with DateTime.ParseExact():

string ds = "Posted on Thursday, May 1st, 2014 at 10:07 AM";

string[] formats = new string[] {
"MMMM d'st, 'yyyy' at 'hh:mm tt",   // for parsing like "1st"
"MMMM d'nd, 'yyyy' at 'hh:mm tt",   // for parsing like "2nd"
"MMMM d'rd, 'yyyy' at 'hh:mm tt",   // for parsing like "3rd"
"MMMM d'th, 'yyyy' at 'hh:mm tt",   // for parsing like "4th"
};

// Get rid of the "Posted on Thursday"
ds = ds.Substring(ds.IndexOf(", ") + 2);
DateTime date = DateTime.ParseExact(ds, formats, null, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal);

// Once you have a DateTime, you can format it any way you want
Console.WriteLine(date.ToString("o"));

Upvotes: 1

Adil
Adil

Reputation: 148120

You can make a custom function and use Custom Date and Time Format Strings to parse the date using DateTime.TryParseExact.

public DateTime ParseDateTime(string dt)
{
    DateTime d;
    if (DateTime.TryParseExact(dt, "'Posted on' dddd, MMM d'st', yyyy 'at' hh:mm tt" , null, System.Globalization.DateTimeStyles.None, out d))  
        return d;   
    if (DateTime.TryParseExact(dt, "'Posted on' dddd, MMM d'nd', yyyy 'at' hh:mm tt", null, System.Globalization.DateTimeStyles.None, out d))
        return d;
    if (DateTime.TryParseExact(dt, "'Posted on' dddd, MMM d'rt', yyyy 'at' hh:mm tt", null, System.Globalization.DateTimeStyles.None, out d))
        return d;
    if (DateTime.TryParseExact(dt, "'Posted on' dddd, MMM d'th', yyyy 'at' hh:mm tt", null, System.Globalization.DateTimeStyles.None, out d))
        return d;

    throw new InvalidOperationException("Not a valid DateTime string");
}

There are few things that need explanation in the datetime string you have.

  1. A literal Posted on, which could be parse by enclosing in single quote.

  2. 1st, which is parse using d'st' and this will apply to 2nd, 3rd, 4th and so on.

Upvotes: 1

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

I would suggest you to split the string and extract the required parts.

below solution only works when the string is in the form of Posted on Thursday, May 1st, 2014 at 10:07 AM

Try This:

string datestring = "Posted on Thursday, May 1st, 2014 at 10:07 AM";
var dateParts = datestring.Split();
var day=dateParts[2].Replace(",","");
var month=dateParts[3];
var date=dateParts[4].Replace("st","").Replace("nd","").Replace("rd","")
                                              .Replace("th","").Replace(",","");
var year=dateParts[5];
var hoursminutes=dateParts[7];
var AMorPM=dateParts[8];

//format d-MMMM-yyyy h:mm tt
string editedDate=date+"-"+month+"-"+year+" "+hoursminutes+" "+AMorPM;
DateTime finalDate = DateTime.ParseExact(editedDate, "d-MMMM-yyyy h:mm tt", 
                                                 CultureInfo.InvariantCulture);

Upvotes: 1

Related Questions