本文整理汇总了C#中System.TimeSpan.TryParseExact方法的典型用法代码示例。如果您正苦于以下问题:C# TimeSpan.TryParseExact方法的具体用法?C# TimeSpan.TryParseExact怎么用?C# TimeSpan.TryParseExact使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.TimeSpan
的用法示例。
在下文中一共展示了TimeSpan.TryParseExact方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
string intervalString, format;
TimeSpan interval;
CultureInfo culture = null;
// Parse hour:minute value with custom format specifier.
intervalString = "17:14";
format = "h\\:mm";
culture = CultureInfo.CurrentCulture;
if (TimeSpan.TryParseExact(intervalString, format,
culture, TimeSpanStyles.AssumeNegative, out interval))
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
else
Console.WriteLine("Unable to parse '{0}' using format {1}",
intervalString, format);
// Parse hour:minute:second value with "g" specifier.
intervalString = "17:14:48";
format = "g";
culture = CultureInfo.InvariantCulture;
if (TimeSpan.TryParseExact(intervalString, format,
culture, TimeSpanStyles.AssumeNegative, out interval))
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
else
Console.WriteLine("Unable to parse '{0}' using format {1}",
intervalString, format);
// Parse hours:minute.second value with custom format specifier.
intervalString = "17:14:48.153";
format = @"h\:mm\:ss\.fff";
culture = null;
if (TimeSpan.TryParseExact(intervalString, format,
culture, TimeSpanStyles.AssumeNegative, out interval))
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
else
Console.WriteLine("Unable to parse '{0}' using format {1}",
intervalString, format);
// Parse days:hours:minute.second value with "G" specifier
// and current (en-US) culture.
intervalString = "3:17:14:48.153";
format = "G";
culture = CultureInfo.CurrentCulture;
if (TimeSpan.TryParseExact(intervalString, format,
culture, TimeSpanStyles.AssumeNegative, out interval))
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
else
Console.WriteLine("Unable to parse '{0}' using format {1}",
intervalString, format);
// Parse days:hours:minute.second value with a custom format specifier.
intervalString = "3:17:14:48.153";
format = @"d\:hh\:mm\:ss\.fff";
culture = null;
if (TimeSpan.TryParseExact(intervalString, format,
culture, TimeSpanStyles.AssumeNegative, out interval))
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
else
Console.WriteLine("Unable to parse '{0}' using format {1}",
intervalString, format);
// Parse days:hours:minute.second value with "G" specifier
// and fr-FR culture.
intervalString = "3:17:14:48,153";
format = "G";
culture = new CultureInfo("fr-FR");
if (TimeSpan.TryParseExact(intervalString, format,
culture, TimeSpanStyles.AssumeNegative, out interval))
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
else
Console.WriteLine("Unable to parse '{0}' using format {1}",
intervalString, format);
// Parse a single number using the "c" standard format string.
intervalString = "12";
format = "c";
if (TimeSpan.TryParseExact(intervalString, format,
null, TimeSpanStyles.AssumeNegative, out interval))
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
else
Console.WriteLine("Unable to parse '{0}' using format {1}",
intervalString, format);
// Parse a single number using the "%h" custom format string.
format = "%h";
if (TimeSpan.TryParseExact(intervalString, format,
null, TimeSpanStyles.AssumeNegative, out interval))
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
else
Console.WriteLine("Unable to parse '{0}' using format {1}",
intervalString, format);
// Parse a single number using the "%s" custom format string.
format = "%s";
if (TimeSpan.TryParseExact(intervalString, format,
null, TimeSpanStyles.AssumeNegative, out interval))
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
else
Console.WriteLine("Unable to parse '{0}' using format {1}",
intervalString, format);
}
}
输出:
'17:14' (h\:mm) --> -17:14:00 '17:14:48' (g) --> 17:14:48 '17:14:48.153' (h\:mm\:ss\.fff) --> -17:14:48.1530000 '3:17:14:48.153' (G) --> 3.17:14:48.1530000 '3:17:14:48.153' (d\:hh\:mm\:ss\.fff) --> -3.17:14:48.1530000 '3:17:14:48,153' (G) --> 3.17:14:48.1530000 '12' (c) --> 12.00:00:00 '12' (%h) --> -12:00:00 '12' (%s) --> -00:00:12
示例2: Main
//引入命名空间
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
string[] inputs = { "3", "16:42", "1:6:52:35.0625",
"1:6:52:35,0625" };
string[] formats = { "g", "G", "%h"};
TimeSpan interval;
CultureInfo culture = new CultureInfo("fr-FR");
// Parse each string in inputs using formats and the fr-FR culture.
foreach (string input in inputs) {
if(TimeSpan.TryParseExact(input, formats, culture, out interval))
Console.WriteLine("{0} --> {1:c}", input, interval);
else
Console.WriteLine("Unable to parse {0}", input);
}
}
}
输出:
3 --> 03:00:00 16:42 --> 16:42:00 Unable to parse 1:6:52:35.0625 1:6:52:35,0625 --> 1.06:52:35.0625000
示例3: Main
//引入命名空间
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
string[] inputs = { "3", "16:42", "1:6:52:35.0625",
"1:6:52:35,0625" };
string[] formats = { "%h", "g", "G" };
TimeSpan interval;
CultureInfo culture = new CultureInfo("fr-FR");
// Parse each string in inputs using formats and the fr-FR culture.
foreach (string input in inputs) {
if(TimeSpan.TryParseExact(input, formats, culture,
TimeSpanStyles.AssumeNegative, out interval))
Console.WriteLine("{0} --> {1:c}", input, interval);
else
Console.WriteLine("Unable to parse {0}", input);
}
}
}
输出:
3 --> -03:00:00 16:42 --> 16:42:00 Unable to parse 1:6:52:35.0625 1:6:52:35,0625 --> 1.06:52:35.0625000
示例4: Main
//引入命名空间
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
string intervalString, format;
TimeSpan interval;
CultureInfo culture;
// Parse hour:minute value with "g" specifier current culture.
intervalString = "17:14";
format = "g";
culture = CultureInfo.CurrentCulture;
if (TimeSpan.TryParseExact(intervalString, format, culture, out interval))
Console.WriteLine("'{0}' --> {1}", intervalString, interval);
else
Console.WriteLine("Unable to parse {0}", intervalString);
// Parse hour:minute:second value with "G" specifier.
intervalString = "17:14:48";
format = "G";
culture = CultureInfo.InvariantCulture;
if (TimeSpan.TryParseExact(intervalString, format, culture, out interval))
Console.WriteLine("'{0}' --> {1}", intervalString, interval);
else
Console.WriteLine("Unable to parse {0}", intervalString);
// Parse hours:minute.second value with "G" specifier
// and current (en-US) culture.
intervalString = "17:14:48.153";
format = "G";
culture = CultureInfo.CurrentCulture;
if (TimeSpan.TryParseExact(intervalString, format, culture, out interval))
Console.WriteLine("'{0}' --> {1}", intervalString, interval);
else
Console.WriteLine("Unable to parse {0}", intervalString);
// Parse days:hours:minute.second value with "G" specifier
// and current (en-US) culture.
intervalString = "3:17:14:48.153";
format = "G";
culture = CultureInfo.CurrentCulture;
if (TimeSpan.TryParseExact(intervalString, format, culture, out interval))
Console.WriteLine("'{0}' --> {1}", intervalString, interval);
else
Console.WriteLine("Unable to parse {0}", intervalString);
// Parse days:hours:minute.second value with "G" specifier
// and fr-FR culture.
intervalString = "3:17:14:48.153";
format = "G";
culture = new CultureInfo("fr-FR");
if (TimeSpan.TryParseExact(intervalString, format, culture, out interval))
Console.WriteLine("'{0}' --> {1}", intervalString, interval);
else
Console.WriteLine("Unable to parse {0}", intervalString);
// Parse days:hours:minute.second value with "G" specifier
// and fr-FR culture.
intervalString = "3:17:14:48,153";
format = "G";
if (TimeSpan.TryParseExact(intervalString, format, culture, out interval))
Console.WriteLine("'{0}' --> {1}", intervalString, interval);
else
Console.WriteLine("Unable to parse {0}", intervalString);
// Parse a single number using the "c" standard format string.
intervalString = "12";
format = "c";
if (TimeSpan.TryParseExact(intervalString, format, null, out interval))
Console.WriteLine("'{0}' --> {1}", intervalString, interval);
else
Console.WriteLine("Unable to parse {0}", intervalString);
// Parse a single number using the "%h" custom format string.
format = "%h";
if (TimeSpan.TryParseExact(intervalString, format, null, out interval))
Console.WriteLine("'{0}' --> {1}", intervalString, interval);
else
Console.WriteLine("Unable to parse {0}", intervalString);
// Parse a single number using the "%s" custom format string.
format = "%s";
if (TimeSpan.TryParseExact(intervalString, format, null, out interval))
Console.WriteLine("'{0}' --> {1}", intervalString, interval);
else
Console.WriteLine("Unable to parse {0}", intervalString);
}
}
输出:
'17:14' --> 17:14:00 Unable to parse 17:14:48 Unable to parse 17:14:48.153 '3:17:14:48.153' --> 3.17:14:48.1530000 Unable to parse 3:17:14:48.153 '3:17:14:48,153' --> 3.17:14:48.1530000 '12' --> 12.00:00:00 '12' --> 12:00:00 '12' --> 00:00:12