I'm struggling a little bit in C# with DateTime.TryParse().

Essentially, given a string I need to extract the year and/or month and day in the current display culture. Sometimes I only get a year, or a month, or all three. Depending on what I get, I have a different control flow.

So far, I managed to parse a variety of strings into a DateTime; that isn't my problem.

My problem is that I wish to know WHAT was actually parsed (i.e. did I get a month or a year, or both).

The uninitialized DateTime defaults to 01/01/0001, and I cannot set everything to an invalid date, such as 99/99/9999 and then see what was filled.

I was thinking maybe I need to do regex, but the DateTime class provides that parsing for multiple cultures, which is very important in this project.

I've tried searching for this, but maybe I'm not using the right terms, because surely someone else must have had this issue before.

Update: Here's some sample code of what I've got:

string strIn = Console.ReadLine();
DateTimeStyles enStyles = DateTimeStyles.AllowInnerWhite | DateTimeStyles.AllowLeadingWhite | DateTimeStyles.AllowTrailingWhite | DateTimeStyles.AssumeLocal;
bFound = DateTime.TryParse(strIn, CultureInfo.CreateSpecificCulture("en-US"), enStyles, out cDT);

Now, bFound will be true if something was parsed successfully. However, I need to know which parts of the date were parsed successfully

有帮助吗?

解决方案

I dont understand you but are you looking for a specified format for your datetime?

string dateAndTimeFormat = "yyyy.MM.dd HH:mm:ss:fff"; // example of format

string dateAndTime = yourdatetimevalue;

DateTime toDateTime = DateTime.ParseExact(dateTime, dateTimeFormat, System.Globalization.CultureInfo.InvariantCulture)

How formats are used: http://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.71).aspx

EDIT 1

The tryparse returns true or false. False if it fails. Maybee that can be usefull? Otherwise you can set the culture before the tryparse, if you are able to do so.

DateTime.TryParse(dateString, culture, styles, out dateResult)

Check the examples here :http://msdn.microsoft.com/en-us/library/9h21f14e.aspx

Under remarks:

"This method tries to ignore unrecognized data, if possible, and fills in missing month, day, and year information with the current date. If s contains only a date and no time, this method assumes the time is 12:00 midnight. If s includes a date component with a two-digit year, it is converted to a year in the current culture's current calendar based on the value of the Calendar.TwoDigitYearMax property. Any leading, inner, or trailing white space character in s is ignored. The date and time can be bracketed with a pair of leading and trailing NUMBER SIGN characters ('#', U+0023), and can be trailed with one or more NULL characters (U+0000)."

Hope some of that helps.

其他提示

The DateTime.TryParse() returns value only on success. So for below code example the variable dt is initialized to 01/01/0001 00:00:00 when declared. When TryParse tries to extract date from string(MM/DD/YYYY format), and if it failes, then dt variable is having value 01/01/0001 00:00:00. Otherwise dt will contain the actual extracted datetime value (as in 2).

1)

DateTime dt;
DateTime.TryParse("23/15/2013", out dt);
// dt contains "01/01/0001 00:00:00"`

2)

`DateTime dt;
DateTime.TryParse("23/12/2013 6:25", out dt);
// dt contains "23/12/2013 06:25:00"`

There is no need to check WHAT was actually parsed. Datetime value will be parsed if it's valid otherwise default datetime value will be returned.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top