本文整理匯總了C#中System.DateTime.TryParse方法的典型用法代碼示例。如果您正苦於以下問題:C# DateTime.TryParse方法的具體用法?C# DateTime.TryParse怎麽用?C# DateTime.TryParse使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.DateTime
的用法示例。
在下文中一共展示了DateTime.TryParse方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Main
//引入命名空間
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
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",
"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);
}
}
}
輸出:
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' to 5/1/2008 12:00:00 AM (Unspecified). 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).
示例2: Main
//引入命名空間
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
string dateString;
CultureInfo culture;
DateTimeStyles styles;
DateTime dateResult;
// Parse a date and time with no styles.
dateString = "03/01/2009 10:00 AM";
culture = CultureInfo.CreateSpecificCulture("en-US");
styles = DateTimeStyles.None;
if (DateTime.TryParse(dateString, culture, styles, out dateResult))
Console.WriteLine("{0} converted to {1} {2}.",
dateString, dateResult, dateResult.Kind);
else
Console.WriteLine("Unable to convert {0} to a date and time.",
dateString);
// Parse the same date and time with the AssumeLocal style.
styles = DateTimeStyles.AssumeLocal;
if (DateTime.TryParse(dateString, culture, styles, out dateResult))
Console.WriteLine("{0} converted to {1} {2}.",
dateString, dateResult, dateResult.Kind);
else
Console.WriteLine("Unable to convert {0} to a date and time.", dateString);
// Parse a date and time that is assumed to be local.
// This time is five hours behind UTC. The local system's time zone is
// eight hours behind UTC.
dateString = "2009/03/01T10:00:00-5:00";
styles = DateTimeStyles.AssumeLocal;
if (DateTime.TryParse(dateString, culture, styles, out dateResult))
Console.WriteLine("{0} converted to {1} {2}.",
dateString, dateResult, dateResult.Kind);
else
Console.WriteLine("Unable to convert {0} to a date and time.", dateString);
// Attempt to convert a string in improper ISO 8601 format.
dateString = "03/01/2009T10:00:00-5:00";
if (DateTime.TryParse(dateString, culture, styles, out dateResult))
Console.WriteLine("{0} converted to {1} {2}.",
dateString, dateResult, dateResult.Kind);
else
Console.WriteLine("Unable to convert {0} to a date and time.", dateString);
// Assume a date and time string formatted for the fr-FR culture is the local
// time and convert it to UTC.
dateString = "2008-03-01 10:00";
culture = CultureInfo.CreateSpecificCulture("fr-FR");
styles = DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeLocal;
if (DateTime.TryParse(dateString, culture, styles, out dateResult))
Console.WriteLine("{0} converted to {1} {2}.",
dateString, dateResult, dateResult.Kind);
else
Console.WriteLine("Unable to convert {0} to a date and time.", dateString);
}
}
輸出:
03/01/2009 10:00 AM converted to 3/1/2009 10:00:00 AM Unspecified. 03/01/2009 10:00 AM converted to 3/1/2009 10:00:00 AM Local. 2009/03/01T10:00:00-5:00 converted to 3/1/2009 7:00:00 AM Local. Unable to convert 03/01/2009T10:00:00-5:00 to a date and time. 2008-03-01 10:00 converted to 3/1/2008 6:00:00 PM Utc.