此方法用於將當前DateTime對象的值轉換為其等效的短時間字符串表示形式。
用法: public string ToShortTimeString ();
返回值:此方法返回一個字符串,其中包含當前DateTime對象的短時間字符串表示形式。
以下示例程序旨在說明DateTime.ToShortTimeString()方法的使用:
示例1:
// C# program to demonstrate the
// DateTime.ToShortTimeString()
// Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
try {
// creating object of DateTime
DateTime date = new DateTime(2010, 1,
1, 4, 0, 15);
// getting ShortTime from DateTime
// using ToShortTimeString() method;
string value = date.ToShortTimeString();
// Display the ShortTime
Console.WriteLine("ShortTime is {0}", value);
}
catch (FormatException e)
{
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
輸出:
ShortTime is 04:00
示例2:
// C# program to demonstrate the
// DateTime.ToShortTimeString()
// Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
// calling check() method
check(new DateTime(2010, 1,
3, 4, 0, 15));
check(new DateTime(2010, 1,
5, 4, 0, 15));
}
public static void check(DateTime date)
{
// getting ShortTime from DateTime
// using ToShortTimeString() method;
string value = date.ToShortTimeString();
// Display the ShortTime
Console.WriteLine("ShortTime of {0}"+
" is {1}", date, value);
}
}
輸出:
ShortTime of 01/03/2010 04:00:15 is 04:00 ShortTime of 01/05/2010 04:00:15 is 04:00
參考:
相關用法
- C# Uri.IsBaseOf(Uri)用法及代碼示例
- C# Random.Next()用法及代碼示例
- C# Uri.ToString()用法及代碼示例
- C# Uri.IsWellFormedOriginalString()用法及代碼示例
- C# Uri.GetHashCode()用法及代碼示例
- C# Math.Log()用法及代碼示例
- C# Uri.FromHex()用法及代碼示例
- C# Uri.IsHexDigit()用法及代碼示例
- C# Queue.Contains()用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 DateTime.ToShortTimeString() Method in C#。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。