DateTime.ToFileTimeUtc() Method in C#

The DateTime.ToFileTimeUtc() method in C# converts the value of the current DateTime object to 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 method is particularly useful when working with file systems, as Windows uses this format internally to store file timestamps. The method automatically converts the DateTime to UTC before performing the conversion.

Syntax

Following is the syntax −

public long ToFileTimeUtc();

Return Value

This method returns a long value representing the Windows file time equivalent of the DateTime object in UTC.

How It Works

DateTime to Windows File Time Conversion DateTime 2019-05-10 06:10:25 ToFileTimeUtc() File Time 132019422250000000 (100ns intervals) Reference Point: January 1, 1601 12:00 AM UTC File time counts 100-nanosecond intervals since this date

Using ToFileTimeUtc() with Specific DateTime

Example

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);
      
      // Convert back to verify
      DateTime converted = DateTime.FromFileTimeUtc(res);
      Console.WriteLine("Converted back = {0}", converted);
   }
}

The output of the above code is −

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

Using ToFileTimeUtc() with Current DateTime

Example

using System;

public class Demo {
   public static void Main() {
      DateTime d = new DateTime(2019, 10, 16, 8, 25, 58);
      Console.WriteLine("Date = {0}", d);
      long res = d.ToFileTimeUtc();
      Console.WriteLine("Windows file time (UTC) = {0}", res);
      
      // Show the difference between local and UTC file times
      DateTime utcTime = d.ToUniversalTime();
      Console.WriteLine("UTC equivalent = {0}", utcTime);
   }
}

The output of the above code is −

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

Practical File System Usage

Example

using System;
using System.IO;

public class Demo {
   public static void Main() {
      // Simulate file creation time
      DateTime fileCreated = new DateTime(2023, 12, 15, 14, 30, 0);
      long fileTime = fileCreated.ToFileTimeUtc();
      
      Console.WriteLine("File created: {0}", fileCreated);
      Console.WriteLine("File time value: {0}", fileTime);
      
      // Calculate days since file creation
      DateTime now = new DateTime(2024, 1, 10, 10, 0, 0);
      TimeSpan difference = now - fileCreated;
      Console.WriteLine("Days since creation: {0}", difference.Days);
      
      // Show file time in readable format
      DateTime readable = DateTime.FromFileTimeUtc(fileTime);
      Console.WriteLine("Readable file time: {0}", readable);
   }
}

The output of the above code is −

File created: 12/15/2023 2:30:00 PM
File time value: 133469598000000000
Days since creation: 26
Readable file time: 12/15/2023 2:30:00 PM

Key Rules

  • The method always converts to UTC before calculating the file time, regardless of the DateTime's Kind property.

  • The returned value represents 100-nanosecond intervals since January 1, 1601 UTC.

  • Use DateTime.FromFileTimeUtc() to convert the file time back to a DateTime object.

  • File times are commonly used in Windows file system operations and registry entries.

Conclusion

The DateTime.ToFileTimeUtc() method converts DateTime objects to Windows file time format, which represents time as 100-nanosecond intervals since January 1, 1601 UTC. This method is essential for file system operations and working with Windows timestamps in applications.

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

283 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements