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
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)
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
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.
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).