Convert the specified Windows file time to an equivalent local time in C#

To convert the specified Windows file time to an equivalent local time in C#, you can use the DateTimeOffset.FromFileTime() method. This method converts a Windows file time (represented as a 64-bit integer) to a DateTimeOffset value that represents the equivalent local time.

Windows file time is measured as the number of 100-nanosecond intervals that have elapsed since January 1, 1601 UTC. The FromFileTime method automatically adjusts the result to the local time zone of the system.

Syntax

Following is the syntax for converting Windows file time to local time −

DateTimeOffset localTime = DateTimeOffset.FromFileTime(fileTime);

Parameters

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

Return Value

Returns a DateTimeOffset object that represents the date and time equivalent to the Windows file time parameter, adjusted to the local time zone.

Using FromFileTime with Zero Value

When you pass 0 as the file time, it represents the Windows file time epoch −

using System;

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

The output of the above code is −

DateTimeOffset = 01 January 1601, 12:00:00
Full format: 1/1/1601 12:00:00 AM +00:00

Using FromFileTime with Custom Values

Here's an example with a larger file time value representing 20 seconds after the epoch −

using System;

public class Demo {
    public static void Main() {
        DateTimeOffset offset = DateTimeOffset.FromFileTime(200000000);
        Console.WriteLine("DateTimeOffset = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss}", offset);
        Console.WriteLine("Ticks: " + offset.Ticks);
        Console.WriteLine("UTC Time: " + offset.UtcDateTime);
    }
}

The output of the above code is −

DateTimeOffset = 01 January 1601, 12:00:20
Ticks: 504911232000000000
UTC Time: 1/1/1601 12:00:20 PM

Converting Current File Time

You can also work with current file times by first getting the current time as file time and then converting it back −

using System;

public class Demo {
    public static void Main() {
        DateTime currentTime = DateTime.Now;
        long fileTime = currentTime.ToFileTime();
        DateTimeOffset converted = DateTimeOffset.FromFileTime(fileTime);
        
        Console.WriteLine("Original time: " + currentTime);
        Console.WriteLine("File time (ticks): " + fileTime);
        Console.WriteLine("Converted back: " + converted);
    }
}

The output of the above code is −

Original time: 12/15/2023 3:30:45 PM
File time (ticks): 133468086450000000
Converted back: 12/15/2023 3:30:45 PM -05:00

Conclusion

The DateTimeOffset.FromFileTime() method provides an easy way to convert Windows file time values to local time. It automatically handles the conversion from the Windows epoch (January 1, 1601 UTC) and adjusts the result to your system's local time zone.

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

308 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements