当前位置: 首页>>代码示例>>C#>>正文


C# TimeSpan.TryParse方法代码示例

本文整理汇总了C#中System.TimeSpan.TryParse方法的典型用法代码示例。如果您正苦于以下问题:C# TimeSpan.TryParse方法的具体用法?C# TimeSpan.TryParse怎么用?C# TimeSpan.TryParse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.TimeSpan的用法示例。


在下文中一共展示了TimeSpan.TryParse方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ParseTimeSpan

//引入命名空间
using System;

public class TryParse
{
   private static void ParseTimeSpan(string intervalStr)
   {
      // Write the first part of the output line.
      Console.Write( "{0,20}   ", intervalStr );

      // Parse the parameter, and then convert it back to a string.
      TimeSpan intervalVal; 
      if (TimeSpan.TryParse(intervalStr, out intervalVal)) 
      {
         string intervalToStr = intervalVal.ToString();
  
         // Pad the end of the TimeSpan string with spaces if it 
         // does not contain milliseconds.
         int pIndex = intervalToStr.IndexOf(':');
         pIndex = intervalToStr.IndexOf('.', pIndex);
         if (pIndex < 0)
            intervalToStr += "        ";
   
         Console.WriteLine("{0,21}", intervalToStr);
         // Handle failure of TryParse method.
      }
      else
      {
         Console.WriteLine("Parse operation failed.");
      }
   } 
   
   public static void Main()
   {
        Console.WriteLine( "{0,20}   {1,21}", 
            "String to Parse", "TimeSpan" );    
        Console.WriteLine( "{0,20}   {1,21}", 
            "---------------", "---------------------" );    

        ParseTimeSpan("0");
        ParseTimeSpan("14");
        ParseTimeSpan("1:2:3");
        ParseTimeSpan("0:0:0.250");
        ParseTimeSpan("10.20:30:40.50");
        ParseTimeSpan("99.23:59:59.9999999");
        ParseTimeSpan("0023:0059:0059.0099");
        ParseTimeSpan("23:0:0");
        ParseTimeSpan("24:0:0");
        ParseTimeSpan("0:59:0");
        ParseTimeSpan("0:60:0");
        ParseTimeSpan("0:0:59");
        ParseTimeSpan("0:0:60");
        ParseTimeSpan("10:");
        ParseTimeSpan("10:0");
        ParseTimeSpan(":10");
        ParseTimeSpan("0:10");
        ParseTimeSpan("10:20:");
        ParseTimeSpan("10:20:0");
        ParseTimeSpan(".123");
        ParseTimeSpan("0.12:00");
        ParseTimeSpan("10.");
        ParseTimeSpan("10.12");
        ParseTimeSpan("10.12:00");
   }
}
//            String to Parse                TimeSpan
//            ---------------   ---------------------
//                          0        00:00:00
//                         14     14.00:00:00
//                      1:2:3        01:02:03
//                  0:0:0.250        00:00:00.2500000
//             10.20:30:40.50     10.20:30:40.5000000
//        99.23:59:59.9999999     99.23:59:59.9999999
//        0023:0059:0059.0099        23:59:59.0099000
//                     23:0:0        23:00:00
//                     24:0:0   Parse operation failed.
//                     0:59:0        00:59:00
//                     0:60:0   Parse operation failed.
//                     0:0:59        00:00:59
//                     0:0:60   Parse operation failed.
//                        10:   Parse operation failed.
//                       10:0        10:00:00
//                        :10   Parse operation failed.
//                       0:10        00:10:00
//                     10:20:   Parse operation failed.
//                    10:20:0        10:20:00
//                       .123   Parse operation failed.
//                    0.12:00        12:00:00
//                        10.   Parse operation failed.
//                      10.12   Parse operation failed.
//                   10.12:00     10.12:00:00
开发者ID:.NET开发者,项目名称:System,代码行数:91,代码来源:TimeSpan.TryParse

示例2: if

string value = "000000006";
TimeSpan interval;
if (TimeSpan.TryParse(value, out interval))
   Console.WriteLine("{0} --> {1}", value, interval);
else
   Console.WriteLine("Unable to parse '{0}'", value);
   
// Output from .NET Framework 3.5 and earlier versions:
//       000000006 --> 6.00:00:00
// Output from .NET Framework 4:
//       Unable to parse //000000006//
开发者ID:.NET开发者,项目名称:System,代码行数:11,代码来源:TimeSpan.TryParse

示例3: Main

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

public class Example
{
   public static void Main()
   {
      string[] values = { "6", "6:12", "6:12:14", "6:12:14:45", 
                          "6.12:14:45", "6:12:14:45.3448", 
                          "6:12:14:45,3448", "6:34:14:45" };
      CultureInfo[] cultures = { new CultureInfo("en-US"), 
                                 new CultureInfo("ru-RU"),
                                 CultureInfo.InvariantCulture };
      
      string header = String.Format("{0,-17}", "String");
      foreach (CultureInfo culture in cultures)
         header += culture.Equals(CultureInfo.InvariantCulture) ? 
                      String.Format("{0,20}", "Invariant") :
                      String.Format("{0,20}", culture.Name);

      Console.WriteLine(header);
      Console.WriteLine();
      
      foreach (string value in values)
      {
         Console.Write("{0,-17}", value);
         foreach (CultureInfo culture in cultures)
         {
            TimeSpan interval = new TimeSpan();
            if (TimeSpan.TryParse(value, culture, out interval))
               Console.Write("{0,20}", interval.ToString("c"));
            else
               Console.Write("{0,20}", "Unable to Parse");
         }
         Console.WriteLine();                                
      }
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:39,代码来源:TimeSpan.TryParse

输出:

String                          en-US               ru-RU           Invariant

6                          6.00:00:00          6.00:00:00          6.00:00:00
6:12                         06:12:00            06:12:00            06:12:00
6:12:14                      06:12:14            06:12:14            06:12:14
6:12:14:45                 6.12:14:45          6.12:14:45          6.12:14:45
6.12:14:45                 6.12:14:45          6.12:14:45          6.12:14:45
6:12:14:45.3448    6.12:14:45.3448000     Unable to Parse  6.12:14:45.3448000
6:12:14:45,3448       Unable to Parse  6.12:14:45.3448000     Unable to Parse
6:34:14:45            Unable to Parse     Unable to Parse     Unable to Parse


注:本文中的System.TimeSpan.TryParse方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。