Trind 07
Trind 07

Reputation: 919

How to get a datetime value within a string?

I've got a string that contains a value of datetime:

string myStr = "= '2015-12-01 00:00:00.000'";

How can I check if myStr contains a datetime value?

How can I get only the datetime value from myStr? The datetime value got from myStr should be: "2015-12-01 00:00:00.000".


The sample code that I have tried to do the task:

string myStr = "= '2015-12-01 00:00:00.000'";    
Regex rgx = new Regex(@"\d{2}-\d{2}-\d{4}");
Match mat = rgx.Match(myStr);
if (mat.ToString() != "") //This will check if string contains datetime value
{
     DateTime myDateTime = DateTime.Parse(mat.ToString()); //This will get the datetime value from string
}

Note: The result from the sample code above is: It can't check if myStr contains a datetime value. It can't get the datetime value from myStr.


You could help me to check if a string contains a datetime value, and get the datetime value within a string.

Upvotes: 1

Views: 230

Answers (4)

Shreevardhan
Shreevardhan

Reputation: 12641

Try this

var myStr = "= '2015-12-01 00:00:00.000'";
var match = Regex.Matches(myStr, @"'(.*?)'")[0].Groups[1].Value;

DateTime result;
if (DateTime.TryParse(match.ToString(), out result)) {
    // Your string has a valid DateTime and it is parsed in result
}
else {
   // Invalid
}

Upvotes: 2

Tetsuya Yamamoto
Tetsuya Yamamoto

Reputation: 24957

If you still want using regex to parse the date:

Regex rgx = new Regex(@"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d{3}");
Match result = rgx.Match(myStr);

if (mat.Success)
{
DateTime myDateTime = DateTime.ParseExact(m.Value, "yyyy-MM-dd hh:mm:ss.fff", CultureInfo.InvariantCulture);
}

CMIIW.

Upvotes: 0

Rob
Rob

Reputation: 27357

There are two things we need to do:

  1. Write a regex which get things that look like they might be a date. However, since dates can be exceedingly complex, we want to delegate that validation to the built-in DateTime library.
  2. For each possible match, we want to validate that it's actually a date.

private IEnumerable<DateTime> GetDates(string str)
{
    var dateSearcherRegex = new Regex(@"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d{3}");

    foreach (Match match in dateSearcherRegex.Matches(str))
    {
        var matchedString = match.Groups[0].Value;
        DateTime date;
        if (DateTime.TryParseExact(matchedString, "yyyy-MM-dd hh:mm:ss.fff", CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
        {
            yield return date;
        }
    }
}

And using it like this:

string myStr = "= '2015-12-01 00:00:00.000'";

var dates = GetDates(myStr);

Upvotes: 1

Akshey Bhat
Akshey Bhat

Reputation: 8545

string myStr = "= '2015-12-01 00:00:00.000'";
 DateTime dt;
 bool b = DateTime.TryParse(myStr.Split(' ')[1].Replace("'",string.Empty),out dt);
     if (b)
     {
          Console.WriteLine("contains datetime");
     }
     else
     {
          Console.WriteLine("doesn't contain datetime");
     }

Use TryParse function to check for DateTime values

Upvotes: 0

Related Questions