Solved

how to parse exact day"th"

Posted on 2011-02-21
5
372 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now