Computer >> Máy Tính >  >> Lập trình >> C#

Phương thức DateTime.ToLongTimeString () trong C #

Phương thức DateTime.ToLongTimeString () trong C # được sử dụng để chuyển đổi giá trị của đối tượng DateTime hiện tại thành biểu diễn chuỗi thời gian dài tương đương của nó.

Cú pháp

Sau đây là cú pháp -

public string ToLongTimeString ();

Ví dụ

Bây giờ chúng ta hãy xem một ví dụ để triển khai phương thức DateTime.ToLongTimeString () -

using System;
using System.Globalization;
public class Demo {
   public static void Main() {
      DateTime d = DateTime.Now;
      Console.WriteLine("Date = {0}", d);
      Console.WriteLine("Current culture = "+CultureInfo.CurrentCulture.Name);
      var pattern = CultureInfo.CurrentCulture.DateTimeFormat;
      string str = d.ToLongTimeString();
      Console.WriteLine("Long time string = {0}", pattern.LongTimePattern);
      Console.WriteLine("Long time string representation = {0}", str);
   }
}

Đầu ra

Điều này sẽ tạo ra kết quả sau -

Date = 10/16/2019 8:41:03 AM
Current culture = en-US
Long time string = h:mm:ss tt
Long time string representation = 8:41:03 AM

Ví dụ

Bây giờ chúng ta hãy xem một ví dụ khác để triển khai phương thức DateTime.ToLongTimeString () -

using System;
public class Demo {
   public static void Main() {
      DateTime d = new DateTime(2019, 11, 11, 7, 11, 25);
      Console.WriteLine("Date = {0}", d);
      string str = d.ToLongTimeString();
      Console.WriteLine("Long time string representation = {0}", str);
   }
}

Đầu ra

Điều này sẽ tạo ra kết quả sau -

Date = 11/11/2019 7:11:25 AM
Long time string representation = 7:11:25 AM