Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

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

Phương thức DateTime.ToOADate() trong C# được dùng để chuyển đổi giá trị của thể hiện này thành ngày OLE Automation tương đương.

Cú pháp

Cú pháp như sau:

public double ToOADate();

Ví dụ 1 - Chuyển đổi DateTime cụ thể

using System;

public class Demo
{
    public static void Main()
    {
        DateTime d = new DateTime(2019, 10, 11, 9, 10, 35);
        Console.WriteLine("Date = {0}", d);
        double res = d.ToOADate();
        Console.WriteLine("OLE Automation date = {0}", res);
    }
}

Kết quả

Date = 10/11/2019 9:10:35 AM
OLE Automation date = 43749.382349537

Ví dụ 2 - Chuyển đổi DateTime.Now

using System;

public class Demo
{
    public static void Main()
    {
        DateTime d = DateTime.Now;
        Console.WriteLine("Date = {0}", d);
        double res = d.ToOADate();
        Console.WriteLine("OLE Automation date = {0}", res);
    }
}

Kết quả

Date = 10/16/2019 8:50:25 AM
OLE Automation date = 43754.3683518634