本文整理汇总了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();
}
}
}
输出:
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();
}
输出:
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