285
Views
1
Comments
Text to Date parser
Question

Hello,

I'm trying to convert text to dates, but the formats could in a lot of varieties.

I have come across this thread tackling the issue from 2017: https://www.outsystems.com/forums/discussion/25442/text-to-date-with-custom-format/

The supplied code in that thread works to a point but falls in some critical areas for my application. For example, it cannot handle January 31st 1992 - even after stripping the superscript. It is also "one and done" piece of code that has not been maintained in any capacity.

Since 2017, has anyone come across better approaches to this problem?

UserImage.jpg
Denis Orlandi de Souza

Develop an c# extension with this regex:

// define regex to clear date:             Regex regex = new Regex("(st|rd|th),");             string date = "January 1st, 2005";             Convert.ToDateTime(regex.Replace(date, ""));             DateTime datetime = Convert.ToDateTime(regex.Replace(date, ""));             Console.Write(datetime.ToString());             date = "January 3rd, 2005";             Convert.ToDateTime(regex.Replace(date, ""));             datetime = Convert.ToDateTime(regex.Replace(date, ""));             Console.Write(datetime.ToString());

     date = "January 6, 2005";             Convert.ToDateTime(regex.Replace(date, ""));             datetime = Convert.ToDateTime(regex.Replace(date, ""));             Console.Write(datetime.ToString());

            date = "January 4th, 2005";             Convert.ToDateTime(regex.Replace(date, ""));             datetime = Convert.ToDateTime(regex.Replace(date, ""));             Console.Write(datetime.ToString());

Source: https://social.msdn.microsoft.com/Forums/vstudio/en-US/8d187d3e-2aa5-49d3-b4da-61f5dc594a62/how-to-convert-the-date-quotjanuary-1st-2005quot?forum=csharpgeneral

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.