Get Date from String

前端 未结 5 574
梦谈多话
梦谈多话 2020-12-16 05:11

Lets say I have one of following strings:

\"Hello, I\'m a String... This is a Stackoverflowquestion!! Here is a Date: 16.03.2013, 02:35 and yeah, plain text          


        
相关标签:
5条回答
  • 2020-12-16 05:20

    For me this code works to get Date from string text contains date.

    var regex = new Regex(@"\d{2}\/\d{2}\/\d{4}");
     foreach (Match m in regex.Matches(line))
     {
      DateTime dt;
      if (DateTime.TryParseExact(m.Value, "MM/dd/yyyy", null, DateTimeStyles.None, out dt))
      remittanceDateArr[chequeNo - 1] = dt.ToString("MM/dd/yyyy");                                   
      rtbExtract.Text = rtbExtract.Text + remittanceDateArr[chequeNo - 1] + "\n";
                                }
    
    0 讨论(0)
  • 2020-12-16 05:21

    This will extract, parse and print all dates in the input text:

    var regex = new Regex(@"\b\d{2}\.\d{2}.\d{4}\b");
    foreach(Match m in regex.Matches(inputText))
    {
        DateTime dt;
        if (DateTime.TryParseExact(m.Value, "dd.MM.yyyy", null, DateTimeStyles.None, out dt))
            Console.WriteLine(dt.ToString());
    }
    

    Now, if you just want the first date, you can do that:

    static DateTime? GetFirstDateFromString(string inputText)
    {
        var regex = new Regex(@"\b\d{2}\.\d{2}.\d{4}\b");
        foreach(Match m in regex.Matches(inputText))
        {
            DateTime dt;
            if (DateTime.TryParseExact(m.Value, "dd.MM.yyyy", null, DateTimeStyles.None, out dt))
                return dt;
        }
        return null;
    }
    

    Note that the method returns a nullable DateTime, so that it can return null when the string contains no date.

    0 讨论(0)
  • 2020-12-16 05:28

    Try this:

    using System;
    using System.Text.RegularExpressions;
    
    public class Example
    {
       public static DateTime? GetFirstDateFromString(string input);
       {
          string pattern = @"\d{2}\.\d{2}\.\d{4}";
          Match m = Regex.Match(input, pattern);
          DateTime result;
          foreach(string value in match.Groups)  
              if (DateTime.TryParseExact(match.Groups[1], "dd.MM.yyyy", CultureInfo.CurrentCulture, DateTimeStyles.None, out result)
                  return result;
          return null;
       }
    }
    
    0 讨论(0)
  • 2020-12-16 05:29

    If your dates are always in that format, you could try using a regex to grab the date string and then use DateTime.ParseExact to get the result you want:

    public DateTime? GetFirstDateFromString(string input)
    {
        DateTime d;
    
        // Exclude strings with no matching substring
        foreach (Match m in Regex.Matches(input, @"[0-9]{2}\.[0-9]{2}\.[0-9]{4}"))
        {
            // Exclude matching substrings which aren't valid DateTimes
            if (DateTime.TryParseExact(match.Value, "dd.MM.yyyy", null, 
                DateTimeStyles.None, out d))
            {
                return d;
            }
        }
        return null;
    }
    
    0 讨论(0)
  • 2020-12-16 05:47

    Create a method whose parameters are a regular expression to catch the date format and the string that you will be extracting the date from. I believe if you don't have a format that will be used then it won't be possible to extract the date from a series of alphanumeric characters in a string.

    0 讨论(0)
提交回复
热议问题