DateTime.TryParse(s, out dt)

[复制链接]
查看11 | 回复2 | 2007-6-22 01:03:37 | 显示全部楼层 |阅读模式
网上看到的DateTime.TryParse(s, out dt)例子,一般是对YY-MM-DD的格式有效。这好像是默认的吧?现在我想让它能对MM-DD—YY也能转换。怎么办?
例如:
DateTime dt;
DateTime.TryParse("02-01-09", out dt);
默认的情况会得到:02年1月9号;我想得到09年2月1号
回复

使用道具 举报

千问 | 2007-6-22 01:03:37 | 显示全部楼层
public static bool TryParse (
string s,
IFormatProvider provider,
DateTimeStyles styles,
out DateTime result
)
怎么用MSDN没有详细的说明,郁闷!
回复

使用道具 举报

千问 | 2007-6-22 01:03:37 | 显示全部楼层
参照如下代码:
string[] dateStrings = {"05/01/2009 14:57:32.8", "2009-05-01 14:57:32.8",

"2009-05-01T14:57:32.8375298-04:00",

"5/01/2008 14:57:32.80 -07:00",

"1 May 2008 2:57:32.8 PM", "16-05-2009 1:00:32 PM",

"Fri, 15 May 2009 20:10:57 GMT" };
DateTime dateValue;
Console.WriteLine("Attempting to parse strings using {0} culture.",

CultureInfo.CurrentCulture.Name);
foreach (string dateString in dateStrings)
{
if (DateTime.TryParse(dateString, out dateValue))
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString,

dateValue, dateValue.Kind);
else
Console.WriteLine("Unable to parse '{0}'.", dateString);
}
// The example displays the following output:
//Attempting to parse strings using en-US culture.
// Converted '05/01/2009 14:57:32.8' to 5/1/2009 2:57:32 PM (Unspecified).
// Converted '2009-05-01 14:57:32.8' to 5/1/2009 2:57:32 PM (Unspecified).
// Converted '2009-05-01T14:57:32.8375298-04:00' to 5/1/2009 11:57:32 AM (Local).
// Converted '5/01/2008 14:57:32.80 -07:00' to 5/1/2008 2:57:32 PM (Local).
// Converted '1 May 2008 2:57:32.8 PM' to 5/1/2008 2:57:32 PM (Unspecified).
// Unable to parse '16-05-2009 1:00:32 PM'.
// Converted 'Fri, 15 May 2009 20:10:57 GMT' to 5/15/2009 1:10:57 PM (Local).
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行