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


C# TimeSpan.ToString方法代碼示例

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


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

示例1: Main

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

public class Example
{
   public static void Main()
   {
      TimeSpan[] intervals = { new TimeSpan(38, 30, 15), 
                               new TimeSpan(16, 14, 30) }; 
      CultureInfo[] cultures = { new CultureInfo("en-US"), 
                                 new CultureInfo("fr-FR") };
      string[] formats = {"c", "g", "G", @"hh\:mm\:ss" };
      Console.WriteLine("{0,12}      Format  {1,22}  {2,22}\n", 
                        "Interval", cultures[0].Name, cultures[1].Name);

      foreach (var interval in intervals) {
         foreach (var fmt in formats)
            Console.WriteLine("{0,12}  {1,10}  {2,22}  {3,22}", 
                              interval, fmt, 
                              interval.ToString(fmt, cultures[0]), 
                              interval.ToString(fmt, cultures[1]));
         Console.WriteLine();
      }  
   }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:26,代碼來源:TimeSpan.ToString

輸出:

Interval      Format                   en-US                   fr-FR

1.14:30:15           c              1.14:30:15              1.14:30:15
1.14:30:15           g              1:14:30:15              1:14:30:15
1.14:30:15           G      1:14:30:15.0000000      1:14:30:15,0000000
1.14:30:15  hh\:mm\:ss                14:30:15                14:30:15

16:14:30           c                16:14:30                16:14:30
16:14:30           g                16:14:30                16:14:30
16:14:30           G      0:16:14:30.0000000      0:16:14:30,0000000
16:14:30  hh\:mm\:ss                16:14:30                16:14:30

示例2: TimeSpan

TimeSpan[] spans = { 
   TimeSpan.Zero, 
   new TimeSpan(-14, 0, 0, 0, 0), 
   new TimeSpan(1, 2, 3), 
   new TimeSpan(0, 0, 0, 0, 250), 
   new TimeSpan(99, 23, 59, 59, 999),
   new TimeSpan(3, 0, 0), 
   new TimeSpan(0, 0, 0, 0, 25) 
};

string[] fmts = { "c", "g", "G", @"hh\:mm\:ss", "%m' min.'" };
foreach (TimeSpan span in spans)
{
   foreach (string fmt in fmts)
      Console.WriteLine("{0}: {1}", fmt, span.ToString(fmt));

   Console.WriteLine();
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:18,代碼來源:TimeSpan.ToString

輸出:

c: 00:00:00
g: 0:00:00
G: 0:00:00:00.0000000
hh\:mm\:ss: 00:00:00
%m' min.': 0 min.

c: -14.00:00:00
g: -14:0:00:00
G: -14:00:00:00.0000000
hh\:mm\:ss: 00:00:00
%m' min.': 0 min.

c: 01:02:03
g: 1:02:03
G: 0:01:02:03.0000000
hh\:mm\:ss: 01:02:03
%m' min.': 2 min.

c: 00:00:00.2500000
g: 0:00:00.25
G: 0:00:00:00.2500000
hh\:mm\:ss: 00:00:00
%m' min.': 0 min.

c: 99.23:59:59.9990000
g: 99:23:59:59.999
G: 99:23:59:59.9990000
hh\:mm\:ss: 23:59:59
%m' min.': 59 min.

c: 03:00:00
g: 3:00:00
G: 0:03:00:00.0000000
hh\:mm\:ss: 03:00:00
%m' min.': 0 min.

c: 00:00:00.0250000
g: 0:00:00.025
G: 0:00:00:00.0250000
hh\:mm\:ss: 00:00:00
%m' min.': 0 min.

示例3: TimeSpan

TimeSpan span;
 
 // Initialize a time span to zero.
 span = TimeSpan.Zero;
 Console.WriteLine(span);

 // Initialize a time span to 14 days.
 span = new TimeSpan(-14, 0, 0, 0, 0);
 Console.WriteLine(span);

 // Initialize a time span to 1:02:03.
 span = new TimeSpan(1, 2, 3);
 Console.WriteLine(span);

 // Initialize a time span to 250 milliseconds.
 span = new TimeSpan(0, 0, 0, 0, 250);
 Console.WriteLine(span);
 
 // Initalize a time span to 99 days, 23 hours, 59 minutes, and 59.999 seconds.
 span = new TimeSpan(99, 23, 59, 59, 999);
 Console.WriteLine(span);
 
 // Initalize a time span to 3 hours.
 span = new TimeSpan(3, 0, 0);
 Console.WriteLine(span);
 
 // Initalize a timespan to 25 milliseconds.
 span = new TimeSpan(0, 0, 0, 0, 25);
 Console.WriteLine(span);

 // The example displays the following output:
 //       00:00:00
 //       -14.00:00:00
 //       01:02:03
 //       00:00:00.2500000
 //       99.23:59:59.9990000
 //       03:00:00
 //       00:00:00.0250000
開發者ID:.NET開發者,項目名稱:System,代碼行數:38,代碼來源:TimeSpan.ToString


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