开发者

Can C# convert these badly formated month/year tags into a set of datetime objects?

I have the following dropdown of in this case auction instances, but there's no standard for naming them: <select name="marketId" onchange="javascript:reload()"><option value="27" selected="selected">Jul08Auc</option> <option value="61">Sep08Auc</option> <option value="623">Dec10Auc</option> <option value="743">Apr11Auc</option> <option value="66">Oct08Auc</option> <option value="81">Nov08Auc</option> <option value="287">October 2009</option> <option value="703">Mar11Auc</option> <option value="222">Jun09Auc</option> <option value="383">Mar10Auc</option> <option value="423">Apr10Auc</option> <option value="523">August 2010</option> <option value="21">Jun08Auc</option> <option value="105">Jan09Auc</option> <option value="142">March 2009</option> <option value="202">May 2009</option> <option value="303">November 2009</option> <option value="449">May10Auc</option> <option value="463"&开发者_如何转开发gt;June 2010</option> <option value="503">July 2010</option> <option value="663">Jan11Auc</option> <option value="763">May11Auc</option> <option value="122">Feb09Auc</option> <option value="282">September 2009</option> <option value="41">Aug08Auc</option> <option value="543">September 2010</option> <option value="603">Nov10Auc</option> <option value="247">July 2009</option> <option value="346">Jan10Auc</option> <option value="363">Feb10</option> <option value="683">Feb11Auc</option> <option value="583">Oct10Auc</option> <option value="101">Dec08Auc</option> <option value="182">April 2009</option> <option value="262">August 2009</option> <option value="323">December 2009</option></select>

Now my question is this, I am going to be getting one of these options back in a post form and I need to change the Month and Year listed in it into a nice DateTime object.

Some of the them are link "Oct10Auc" and some of them are like "December 2009", which means there wasn't any control to the date formatting.

What I want to do is take the "Oct10Auc", have the code look at it and say, ohh yeah you want a date of 10/1/2010, or take September 2009 and say " you want a date of 9/1/2009".

My goal is to NOT have to write a huge string parser that tries to figure out what time of stamp it's looking at and then decode it.

I wonder if C# or any available API can handle this?


I would build a list of formats then call DateTime.TryParseExact for each on.

static DateTime Parse(string dateString)
{
 string[] formats = new [] {"MMMYY\A\u\c", "MMMM YYYY"};
 DateTime parsedDate = new DateTime();
 foreach (string fmt in formats)
 {
    if (DateTime.TryParseExact (dateString, fmt, null, DateTimeStyles.Default, out parsedDate)
       return parsedDate;
 }
 throw new FormatException ();
}

Here is a full list of format options. You can embed escaped characters (like you AUC).


Easy enough to test it in C#. Whip up a quick console app, pass it the date values, and see if DateTime.TryParse() works on them. The one thing you want to do beforehand is string replace the "Auc" with "".

But really it looks like there are only 2 patterns to the dates: Month Year MonYrAuc

DateTime.TryParse() should be fine with Month Year. For MonYr you just need to put a space between the Mon and Yr, then add a "20" in front of the Yr assuming it's just 2000s.


You can use DateTime.ParseExact function. Just create valid format argument, use InvariantCulture as IFormatProvider and pass your string to the function.

As you have two formats, you can catch FormatException exception and you can try to parse date with another format.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜