开发者

converting string representation of a date and time to the System.DateTime

DateTime dt;
bool diditParse = DateTime.TryParse("17/06/2000 12:00:00 AM", CultureInfo.CreateSpecificCulture("en-US"), DateTimeStyles.AssumeLocal, out dt);

diditParse is returning false beca开发者_如何学Gouse it is expecting a format MM/DD/YYYY which differs from what I have DD/MM/YYYY

I am not sure what culture/styles or what needs to be do to get the try parse working?


If you look at the example given here http://msdn.microsoft.com/en-us/library/ch92fbc1.aspx you can just use:

bool diditParse = DateTime.TryParse("17/06/2000 12:00:00 AM", out dt);

Unless your looking for something more indepth you shouldn't need to use the culture/styles part.

Your trying to use http://msdn.microsoft.com/en-us/library/9h21f14e.aspx

public static bool TryParse(
string s,
IFormatProvider provider,
DateTimeStyles styles,
out DateTime result
)

Parameters

s
Type: System.String
A string containing a date and time to convert. 
provider
Type: System.IFormatProvider
An object that supplies culture-specific formatting information about s. 
styles
Type: System.Globalization.DateTimeStyles
A bitwise combination of enumeration values that defines how to interpret the parsed date in relation to the current time zone or the current date. A typical value to specify is None.
result
Type: System.DateTime%
When this method returns, contains the DateTime value equivalent to the date and time contained in s, if the conversion succeeded, or MinValue if the conversion failed. The conversion fails if the s parameter is null, is an empty string (""), or does not contain a valid string representation of a date and time. This parameter is passed uni


If you know that the string is in DD/MM/YY format, then you can use TryParseExact.


Try using DateTime.TryParseExact()

It appears to allow you to tell it what date format to expect.


Try...

("fr-FR") 

although any European or other culture which represents dates in that fashion should work.


Try "en-GB" we list our dates DD-MM-YY, instead of MM-DD-YY like they do in the US.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜