Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Date from String

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 blah blah..-."

"This the other string! :) 22.11.2012. Its a Date you see"

"Here we have 2 Dates, 23.12.2012 and 14.07.2011"

What would be the best and fastest way to get these dates from the string (in DateTime)?

(Only First occured Date in String)

Desirable Returns:

String 1: 16.03.2013 (as a DateTime)
String 2: 22.11.2012 ("           ")
String 3: 23.12.2012 ("           ")

So I would call a method something like:

DateTime date1 = GetFirstDateFromString(string1);
like image 955
eMi Avatar asked Apr 25 '13 19:04

eMi


People also ask

Can we convert string to date?

Using the Parse API With a Custom Formatter. Converting a String with a custom date format into a Date object is a widespread operation in Java. For this purpose we'll use the DateTimeFormatter class, which provides numerous predefined formatters, and allows us to define a formatter.

How do you parse a date?

The parse() method takes a date string (such as "2011-10-10T14:48:00" ) and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC. This function is useful for setting date values based on string values, for example in conjunction with the setTime() method and the Date object.

How do I convert string to date in python?

We can convert a string to datetime using strptime() function. This function is available in datetime and time modules to parse a string to datetime and time objects respectively.


2 Answers

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.

like image 99
Thomas Levesque Avatar answered Sep 24 '22 11:09

Thomas Levesque


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";
                            }
like image 43
Adeel Kamran Avatar answered Sep 23 '22 11:09

Adeel Kamran