Phương thức DateTimeOffset.ToLocalTime () trong C # được sử dụng để chuyển đổi đối tượng DateTimeOffset hiện tại thành một đối tượng DateTimeOffset đại diện cho giờ địa phương.
Cú pháp
Sau đây là cú pháp -
public DateTimeOffset ToLocalTime ();
Ví dụ
Bây giờ chúng ta hãy xem một ví dụ để triển khai phương thức DateTimeOffset.ToLocalTime () -
using System;
public class Demo {
public static void Main() {
DateTimeOffset local;
DateTimeOffset dateTimeOffset = DateTimeOffset.Now;
Console.WriteLine("DateTimeOffset = {0}", dateTimeOffset);
local = dateTimeOffset.ToLocalTime();
Console.WriteLine("Local Time = "+local.ToString());
}
} Đầu ra
Điều này sẽ tạo ra kết quả sau -
DateTimeOffset = 10/16/2019 11:20:35 AM +00:00 Local Time = 10/16/2019 11:20:35 AM +00:00
Ví dụ
Bây giờ chúng ta hãy xem một ví dụ khác để triển khai phương thức DateTimeOffset.ToLocalTime () -
using System;
public class Demo {
public static void Main() {
DateTimeOffset local;
DateTimeOffset dateTimeOffset = new DateTimeOffset(2019, 10, 12, 6, 10, 20, TimeSpan.Zero);
Console.WriteLine("DateTimeOffset = {0}", dateTimeOffset);
local = dateTimeOffset.ToLocalTime();
Console.WriteLine("Local Time = "+local.ToString());
}
} Đầu ra
Điều này sẽ tạo ra kết quả sau -
DateTimeOffset = 10/12/2019 6:10:20 AM +00:00 Local Time = 10/12/2019 6:10:20 AM +00:00