Solved

how to parse exact day"th"

Posted on 2011-02-21
5
374 Views
Last Modified: 2012-08-14
How can I parse exact this string in c#?

It seems it works with DateTime.Parse() as long as the "th" is not there how can I fix this?

Friday 18th February 2011 8:11:04 AM
0
Comment
Question by:NewtonianB
  • 2
  • 2
5 Comments
 
LVL 6

Accepted Solution

by:
Ramone_Hamilton earned 300 total points
ID: 34946325
Just replace the th.

            string date = "Friday 18th February 2011 8:11:04 AM";
            date = date.Replace("th", string.Empty);
            date = date.Replace("rd", string.Empty);
            date = date.Replace("st", string.Empty);

            DateTime.Parse(date);

Open in new window

0
 
LVL 29

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 200 total points
ID: 34947275
Not too bad idea from Ramone_Hamilton but be careful!

The code above may convert the line

Saturday 19th February 2011 8:11:04 AM

to

Satuay 19th February 2011 8:11:04 AM

Same problem with August - you will leave it with "Augu"...

So - the code needs to be a bit more complicated. You may use regular expressions to remove suffixes after digits or just look for digits and then check for suffixes (and remove them if exist).
0
 
LVL 6

Expert Comment

by:Ramone_Hamilton
ID: 34947367
That's a really good catch!  I myself detest regular expressions as they always beat me down.  I know this may be excessive, but what about taking your string, split it by the spaces and then do the replace?

Here is a revised version.

            string date = "Friday 18th February 2011 8:11:04 AM";

            string[] dates = date.Split(' ');

            dates[1] = dates[1].Replace("th", string.Empty);
            dates[1] = dates[1].Replace("rd", string.Empty);
            dates[1] = dates[1].Replace("st", string.Empty);

            StringBuilder newDate = new StringBuilder();

            newDate.Append(dates[0]).Append(" ").Append(dates[1]).Append(" ").Append(dates[2]).Append(" ").Append(
                dates[3]).Append(" ").Append(dates[4]).Append(" ").
                Append(dates[5]);

            DateTime.Parse(newDate.ToString()); 

Open in new window

0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 34947563
OK, I agree - again good idea, I'd go with splitting and replacing dates[1].

However to use StringBuilder to re-assemble the string is an overkill, I think.

I'd write it as :

string cleaneddateStr = dates[0] + " " + dates[1] + " " + dates[2] + " " + dates[3] + " " + dates[4] + " " + dates[5]

Compiler is clever enough to create just a one string here so - no much overhead. With StringBuilder - quite a lot of overhead. I tested this couple of years ago.

Anyway, it works
0
 

Author Comment

by:NewtonianB
ID: 35017585
we forgot "nd" also
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question