Extract multiple dates from String if code finds any matching one of formats

I have a String which might take the form of the following:

  • dd/MM/yyyy - dd/MM/yyyy
  • dd/MM/yyyy to dd/MM/yyyy
  • dd-MM-yyyy - dd-MM-yyyy
  • dd-MM-yyyy to dd-MM-yyyy
  • 10 December 2016 to 20 March 2017
  • em-dash without spaces etc.

and so on with various permutations accounting for typo, various spaces etc. With minimal hardcoding, how do I write a function to extract and return a two element array of DateTime/Date where the first is the start date and the second is the end date? Most important is to just grab any dates which the function can detect

1 Like

Hi:
I don’t have any best idea , maybe you can have a try:
1.trim with /r/n
2. trim with - or to
3.trim space
4. Method1,IsDate(“2018/08/08”)
< dateTime>=System.Convert.ToDateTime(“2018/08/08”)
Method2,
< dateTime>=DateTime.ParseExact(strIn, “MM-dd- yyyy”,System.Globalization.CultureInfo.InvariantCulture)

edit… also includes other things people might type

e.g.
10 December 2016 to 20 March 2017

using em-dash without spaces etc.


It also can be recognized as a dateTime.

some question about English word:
what’s the meaning about : edit… em-dash
Thanks for your reply

em-dash — just a longer dash some people use as a separator

My concern is that trimming spaces, looking character by character or having string patterns to look for (" to ", " - " etc.) will not work well for edge cases that have not been coded into the logic e.g. if we code to split by spaces then “10 December 2016 to 20 March 2017” it will result in {“10”, “December”, “2016”…}

e.g. 15-Dec-17something1abc20 April 2018-something2xyz-12/06/2018 can be parsed to 3 element Date/DateTime array

Hi,

I have an idea replace the (-,to,–, whatever separator coming b/w dates) into any comman character(like #) and split using ‘#’ as a delimiter.

Then we have one library, it will read any date(having any format) and convert to given input format. Please install this library using manage packages.

DateFormat.1.0.6969.26139.nupkg (9.7 KB)

Thanks!

1 Like

Hmm interesting, do you have an example of how this might work?

Thanks

Also, what if the string includes formats

  • dd.MM.yyyy-dd.MM.yyyy
  • dd-MM-yyyy to dd-MM-yyyy

would replacing the ‘-’ affect the second string format?

@DEATHFISH

You can use regular expression to extract the values which are in various date formats. You have to define pattern for every date type which you need to extract.

Refer Sample.xaml (9.4 KB)
I have defined regex patterns for the date formats mentioned in above post. Also i have copied FROM and TO dates into two different List(of string).

1 Like

@ranjith How does the Sample workflow work?

Why do we need two separate lists for From date and To date? What if both From and To dates have the same format?

You have requested for two array.

Using Regex the date has been extracted from the text in the workflow.

Actually I requested for one array with two elements…

How would I modify the workflow to place all dates into one array? Also how do I add more date formats to capture?