DateTimeOffset.ToFileTime() Method in C#

The DateTimeOffset.ToFileTime() method in C# is used to convert the value of the current DateTimeOffset object to a Windows file time. The method returns an Int64 value representing the current DateTimeOffset object, expressed as a Windows file time.

A Windows file time is a 64-bit value that represents the number of 100-nanosecond intervals that have elapsed since 12:00 midnight, January 1, 1601 A.D. (UTC). This format is commonly used by Windows file systems to store file timestamps.

Syntax

Following is the syntax −

public long ToFileTime();

Return Value

The method returns a long value representing the Windows file time equivalent of the DateTimeOffset object.

How It Works

The conversion process involves converting the DateTimeOffset to UTC and then calculating the number of 100-nanosecond intervals since the Windows file time epoch (January 1, 1601).

DateTimeOffset to Windows File Time Conversion DateTimeOffset With timezone offset UTC DateTime Offset removed File Time 100ns intervals since 1601

Examples

Example 1: Converting Specific DateTimeOffset

using System;

public class Demo {
    public static void Main() {
        DateTimeOffset dateTimeOffset1 = new DateTimeOffset(2019, 11, 10, 6, 20, 10, new TimeSpan(-5, 0, 0));
        Console.WriteLine("DateTimeOffset = {0}", dateTimeOffset1);
        
        long res = dateTimeOffset1.ToFileTime();
        Console.WriteLine("Windows file time = {0}", res);
        
        // Convert back to verify
        DateTimeOffset converted = DateTimeOffset.FromFileTime(res);
        Console.WriteLine("Converted back = {0}", converted);
    }
}

The output of the above code is −

DateTimeOffset = 11/10/2019 6:20:10 AM -05:00
Windows file time = 132178584100000000
Converted back = 11/10/2019 11:20:10 AM +00:00

Example 2: Converting Current DateTimeOffset

using System;

public class Demo {
    public static void Main() {
        DateTimeOffset dateTimeOffset1 = DateTimeOffset.Now;
        Console.WriteLine("Current DateTimeOffset = {0}", dateTimeOffset1);
        
        long res = dateTimeOffset1.ToFileTime();
        Console.WriteLine("Windows file time = {0}", res);
        
        // Show the UTC equivalent
        Console.WriteLine("UTC equivalent = {0}", dateTimeOffset1.UtcDateTime);
    }
}

The output of the above code is −

Current DateTimeOffset = 10/16/2019 11:16:47 AM +00:00
Windows file time = 132156982075593442
UTC equivalent = 10/16/2019 11:16:47 AM

Example 3: Comparing Different Timezones

using System;

public class Demo {
    public static void Main() {
        // Same moment in different timezones
        DateTimeOffset utc = new DateTimeOffset(2023, 1, 1, 12, 0, 0, TimeSpan.Zero);
        DateTimeOffset est = new DateTimeOffset(2023, 1, 1, 7, 0, 0, new TimeSpan(-5, 0, 0));
        DateTimeOffset pst = new DateTimeOffset(2023, 1, 1, 4, 0, 0, new TimeSpan(-8, 0, 0));
        
        Console.WriteLine("UTC: {0} -> File Time: {1}", utc, utc.ToFileTime());
        Console.WriteLine("EST: {0} -> File Time: {1}", est, est.ToFileTime());
        Console.WriteLine("PST: {0} -> File Time: {1}", pst, pst.ToFileTime());
        
        Console.WriteLine("All file times are equal: {0}", 
            utc.ToFileTime() == est.ToFileTime() && est.ToFileTime() == pst.ToFileTime());
    }
}

The output of the above code is −

UTC: 1/1/2023 12:00:00 PM +00:00 -> File Time: 133121616000000000
EST: 1/1/2023 7:00:00 AM -05:00 -> File Time: 133121616000000000
PST: 1/1/2023 4:00:00 AM -08:00 -> File Time: 133121616000000000
All file times are equal: True

Common Use Cases

  • File System Operations: Windows file systems use this format for storing file timestamps.

  • Interoperability: When working with Windows APIs that require file time format.

  • Time Comparisons: Converting to a numeric format for easy comparison and arithmetic operations.

Conclusion

The DateTimeOffset.ToFileTime() method converts a DateTimeOffset to Windows file time format, which represents time as 100-nanosecond intervals since January 1, 1601 UTC. This method is particularly useful for file system operations and Windows API interoperability, ensuring consistent time representation regardless of the original timezone offset.

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

145 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements