當前位置: 首頁>>代碼示例>>C#>>正文


C# DateTime.TryParseExact方法代碼示例

本文整理匯總了C#中System.DateTime.TryParseExact方法的典型用法代碼示例。如果您正苦於以下問題:C# DateTime.TryParseExact方法的具體用法?C# DateTime.TryParseExact怎麽用?C# DateTime.TryParseExact使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.DateTime的用法示例。


在下文中一共展示了DateTime.TryParseExact方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: Main

//引入命名空間
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      CultureInfo enUS = new CultureInfo("en-US"); 
      string dateString;
      DateTime dateValue;
      
      // Parse date with no style flags.
      dateString = " 5/01/2009 8:30 AM";
      if (DateTime.TryParseExact(dateString, "g", enUS, 
                                 DateTimeStyles.None, out dateValue))
         Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, 
                           dateValue.Kind);
      else
         Console.WriteLine("'{0}' is not in an acceptable format.", dateString);

      // Allow a leading space in the date string.
      if (DateTime.TryParseExact(dateString, "g", enUS, 
                                 DateTimeStyles.AllowLeadingWhite, out dateValue))
         Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, 
                           dateValue.Kind);
      else
         Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
      
      // Use custom formats with M and MM.
      dateString = "5/01/2009 09:00";
      if (DateTime.TryParseExact(dateString, "M/dd/yyyy hh:mm", enUS, 
                                 DateTimeStyles.None, out dateValue))
         Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, 
                           dateValue.Kind);
      else
         Console.WriteLine("'{0}' is not in an acceptable format.", dateString);

      // Allow a leading space in the date string.
      if (DateTime.TryParseExact(dateString, "MM/dd/yyyy hh:mm", enUS, 
                              DateTimeStyles.None, out dateValue))
         Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, 
                           dateValue.Kind);
      else
         Console.WriteLine("'{0}' is not in an acceptable format.", dateString);

      // Parse a string with time zone information.
      dateString = "05/01/2009 01:30:42 PM -05:00"; 
      if (DateTime.TryParseExact(dateString, "MM/dd/yyyy hh:mm:ss tt zzz", enUS, 
                              DateTimeStyles.None, out dateValue))
         Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, 
                           dateValue.Kind);
      else
         Console.WriteLine("'{0}' is not in an acceptable format.", dateString);

      // Allow a leading space in the date string.
      if (DateTime.TryParseExact(dateString, "MM/dd/yyyy hh:mm:ss tt zzz", enUS, 
                              DateTimeStyles.AdjustToUniversal, out dateValue))
         Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, 
                           dateValue.Kind);
      else
         Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
           
      // Parse a string representing UTC.
      dateString = "2008-06-11T16:11:20.0904778Z";
      if (DateTime.TryParseExact(dateString, "o", CultureInfo.InvariantCulture, 
                                     DateTimeStyles.None, out dateValue))
         Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, 
                           dateValue.Kind);
      else
         Console.WriteLine("'{0}' is not in an acceptable format.", dateString);

      if (DateTime.TryParseExact(dateString, "o", CultureInfo.InvariantCulture, 
                                 DateTimeStyles.RoundtripKind, out dateValue))
         Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, 
                           dateValue.Kind);
      else
         Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
   }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:80,代碼來源:DateTime.TryParseExact

輸出:

' 5/01/2009 8:30 AM' is not in an acceptable format.
Converted ' 5/01/2009 8:30 AM' to 5/1/2009 8:30:00 AM (Unspecified).
Converted '5/01/2009 09:00' to 5/1/2009 9:00:00 AM (Unspecified).
'5/01/2009 09:00' is not in an acceptable format.
Converted '05/01/2009 01:30:42 PM -05:00' to 5/1/2009 11:30:42 AM (Local).
Converted '05/01/2009 01:30:42 PM -05:00' to 5/1/2009 6:30:42 PM (Utc).
Converted '2008-06-11T16:11:20.0904778Z' to 6/11/2008 9:11:20 AM (Local).
Converted '2008-06-11T16:11:20.0904778Z' to 6/11/2008 4:11:20 PM (Utc).

示例2: Main

//引入命名空間
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string[] formats= {"M/d/yyyy h:mm:ss tt", "M/d/yyyy h:mm tt", 
                         "MM/dd/yyyy hh:mm:ss", "M/d/yyyy h:mm:ss", 
                         "M/d/yyyy hh:mm tt", "M/d/yyyy hh tt", 
                         "M/d/yyyy h:mm", "M/d/yyyy h:mm", 
                         "MM/dd/yyyy hh:mm", "M/dd/yyyy hh:mm"};
      string[] dateStrings = {"5/1/2009 6:32 PM", "05/01/2009 6:32:05 PM", 
                              "5/1/2009 6:32:00", "05/01/2009 06:32", 
                              "05/01/2009 06:32:00 PM", "05/01/2009 06:32:00"}; 
      DateTime dateValue;
      
      foreach (string dateString in dateStrings)
      {
         if (DateTime.TryParseExact(dateString, formats, 
                                    new CultureInfo("en-US"), 
                                    DateTimeStyles.None, 
                                    out dateValue))
            Console.WriteLine("Converted '{0}' to {1}.", dateString, dateValue);
         else
            Console.WriteLine("Unable to convert '{0}' to a date.", dateString);
      }
   }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:30,代碼來源:DateTime.TryParseExact

輸出:

Converted '5/1/2009 6:32 PM' to 5/1/2009 6:32:00 PM.
Converted '05/01/2009 6:32:05 PM' to 5/1/2009 6:32:05 PM.
Converted '5/1/2009 6:32:00' to 5/1/2009 6:32:00 AM.
Converted '05/01/2009 06:32' to 5/1/2009 6:32:00 AM.
Converted '05/01/2009 06:32:00 PM' to 5/1/2009 6:32:00 PM.
Converted '05/01/2009 06:32:00' to 5/1/2009 6:32:00 AM.


注:本文中的System.DateTime.TryParseExact方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。