Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

เมธอด DateTime.ToFileTimeUtc() ใน C#

เมธอด DateTime.ToFileTimeUtc() ใน C# แปลงค่าของออบเจ็กต์ DateTime ปัจจุบันให้เป็นเวลาไฟล์ของ Windows (Windows File Time) ในรูปแบบ UTC

ไวยากรณ์

public long ToFileTimeUtc();

ตัวอย่างที่ 1: DateTime เฉพาะเจาะจง

using System;

public class Demo
{
    public static void Main()
    {
        DateTime d = new DateTime(2019, 05, 10, 6, 10, 25);
        
        Console.WriteLine("Date = {0}", d);
        long res = d.ToFileTimeUtc();
        Console.WriteLine("Windows file time (Utc) = {0}", res);
    }
}

ผลลัพธ์

Date = 5/10/2019 6:10:25 AM
Windows file time (Utc) = 132019422250000000

ตัวอย่างที่ 2: ใช้ DateTime.Now

using System;

public class Demo
{
    public static void Main()
    {
        DateTime d = DateTime.Now;
        
        Console.WriteLine("Date = {0}", d);
        long res = d.ToFileTimeUtc();
        Console.WriteLine("Windows file time (Utc) = {0}", res);
    }
}

ผลลัพธ์

Date = 10/16/2019 8:25:58 AM
Windows file time (Utc) = 132156879583999032

คำอธิบาย: Windows File Time คือจำนวนทิก (100-nanosecond intervals) นับจาก 1 มกราคม 1601 (UTC) ใช้ในระบบไฟล์ NTFS