Solved

how to parse exact day"th"

Posted on 2011-02-21
5
371 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
we forgot "nd" also
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 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

11 Experts available now in Live!

Get 1:1 Help Now