DateTime.FromFileTime() Method in C#

The DateTime.FromFileTime() method in C# converts a Windows file time to an equivalent local time. Windows file time represents the number of 100-nanosecond intervals that have elapsed since 12:00 midnight, January 1, 1601 A.D. (UTC).

Syntax

Following is the syntax −

public static DateTime FromFileTime(long fileTime);

Parameters

The method takes one parameter −

  • fileTime − A Windows file time expressed in ticks (100-nanosecond intervals since January 1, 1601 UTC).

Return Value

Returns a DateTime object that represents the local time equivalent of the Windows file time specified by the fileTime parameter.

Windows File Time Conversion Windows File Time Ticks since Jan 1, 1601 UTC Local DateTime Converted to local time zone FromFileTime() method

Using DateTime.FromFileTime() with File Time Values

Example 1: Converting a Large File Time Value

using System;

public class Demo {
    public static void Main() {
        DateTime d1 = DateTime.FromFileTime(100000000000);
        Console.WriteLine("DateTime = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss}", d1);
        Console.WriteLine("Full DateTime: " + d1.ToString());
    }
}

The output of the above code is −

DateTime = 01 January 1601, 02:46:40
Full DateTime: 1/1/1601 2:46:40 AM

Example 2: Converting Zero File Time

using System;

public class Demo {
    public static void Main() {
        DateTime d1 = DateTime.FromFileTime(0);
        Console.WriteLine("DateTime = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss}", d1);
        Console.WriteLine("This represents: " + d1.ToString("F"));
    }
}

The output of the above code is −

DateTime = 01 January 1601, 12:00:00
This represents: Monday, January 1, 1601 12:00:00 PM

Using DateTime.FromFileTime() with Current File Time

Example 3: Working with Current File Time

using System;

public class Demo {
    public static void Main() {
        // Get current time as file time
        long currentFileTime = DateTime.Now.ToFileTime();
        Console.WriteLine("Current file time: " + currentFileTime);
        
        // Convert back to DateTime
        DateTime convertedTime = DateTime.FromFileTime(currentFileTime);
        Console.WriteLine("Converted back: " + convertedTime.ToString("yyyy-MM-dd HH:mm:ss"));
        
        // Compare with original
        Console.WriteLine("Original time: " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
    }
}

The output of the above code is −

Current file time: 133024512000000000
Converted back: 2023-04-15 14:20:00
Original time: 2023-04-15 14:20:00

Key Points

  • Windows file time starts from January 1, 1601 at 12:00:00 midnight UTC.

  • The method converts the file time to the local time zone of the system.

  • Each tick represents 100 nanoseconds (0.1 microseconds).

  • Use DateTime.FromFileTimeUtc() if you need UTC time instead of local time.

Conclusion

The DateTime.FromFileTime() method is essential for converting Windows file timestamps to readable DateTime objects in local time. It's commonly used when working with file system operations or Windows APIs that return file times as tick values.

Updated on: 2026-03-17T07:04:35+05:30

199 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements