📜  C#中的DateTime.ToLongTimeString()方法

📅  最后修改于: 2021-05-29 21:46:19             🧑  作者: Mango

此方法用于将当前DateTime对象的值转换为其等效的长时间字符串表示形式。

下面的程序说明了DateTime.ToLongTimeString()方法的用法:

范例1:

// C# program to demonstrate the
// DateTime.ToLongTimeString()
// Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // creating object of DateTime
        DateTime date = new DateTime(2011, 1,
                                1, 4, 0, 15);
  
        // Converting the value of the 
        // current DateTime object to 
        // its equivalent long time 
        // string representation.
        // using ToLongTimeString() method;
        string value = date.ToLongTimeString();
  
        // Display the time
        Console.WriteLine("String representation of"+
                              " time is {0}", value);
    }
}
输出:
String representation of time is 04:00:15

范例2:

// C# program to demonstrate the
// DateTime.ToLongTimeString()
// Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // creating object of DateTime
        DateTime date = DateTime.Now;
  
        // Converting the value of the
        // current DateTime object to 
        // its equivalent long time 
        // string representation.
        // using ToLongTimeString() method;
        string value = date.ToLongTimeString();
  
        // Display the time
        Console.WriteLine("String representation "+
                          "of time is {0}", value);
    }
}
输出:
String representation of time is 05:30:08

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.datetime.tolongtimestring?view=netframework-4.7.2