Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
DateTime.FromFileTimeUtc() Method in C#
The DateTime.FromFileTimeUtc() method in C# converts a Windows file time to an equivalent UTC DateTime. This method is useful when working with file timestamps or system-level operations that use Windows file time format, which represents time as the number of 100-nanosecond intervals since January 1, 1601 UTC.
Syntax
Following is the syntax −
public static DateTime FromFileTimeUtc(long fileTime);
Parameters
The method accepts the following 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 UTC time equivalent to the specified Windows file time.
Using DateTime.FromFileTimeUtc() with Different Values
Example 1: Basic Usage
using System;
public class Demo {
public static void Main() {
DateTime d1 = DateTime.FromFileTimeUtc(6500000000000);
Console.WriteLine("DateTime = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss}", d1);
}
}
The output of the above code is −
DateTime = 08 January 1601, 12:33:20
Example 2: Multiple File Time Conversions
using System;
public class Demo {
public static void Main() {
DateTime d1 = DateTime.FromFileTimeUtc(0);
DateTime d2 = DateTime.FromFileTimeUtc(850000000000000);
Console.WriteLine("DateTime = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss}", d1);
Console.WriteLine("New DateTime = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss}", d2);
}
}
The output of the above code is −
DateTime = 01 January 1601, 12:00:00 New DateTime = 11 September 1603, 07:06:40
Using DateTime.FromFileTimeUtc() with Current File Times
Example 3: Working with Current Time Values
using System;
public class Demo {
public static void Main() {
DateTime currentUtc = DateTime.UtcNow;
long fileTime = currentUtc.ToFileTimeUtc();
DateTime convertedBack = DateTime.FromFileTimeUtc(fileTime);
Console.WriteLine("Original UTC: {0}", currentUtc.ToString("yyyy-MM-dd HH:mm:ss"));
Console.WriteLine("File Time: {0}", fileTime);
Console.WriteLine("Converted Back: {0}", convertedBack.ToString("yyyy-MM-dd HH:mm:ss"));
Console.WriteLine("Are Equal: {0}", currentUtc.ToString("yyyy-MM-dd HH:mm:ss") == convertedBack.ToString("yyyy-MM-dd HH:mm:ss"));
}
}
The output of the above code is −
Original UTC: 2023-10-15 14:30:45 File Time: 133421478450000000 Converted Back: 2023-10-15 14:30:45 Are Equal: True
Key Points
- Windows file time starts from January 1, 1601 UTC, not the Unix epoch (January 1, 1970).
- Each tick represents 100 nanoseconds (0.1 microseconds).
- The method always returns a UTC DateTime with
Kindset toDateTimeKind.Utc. - Use
ToFileTimeUtc()to convert a DateTime back to Windows file time format.
Conclusion
The DateTime.FromFileTimeUtc() method provides an efficient way to convert Windows file time values to UTC DateTime objects. This is particularly useful when working with file system operations, system APIs, or when interfacing with Windows-based time representations that use the 100-nanosecond tick format starting from January 1, 1601 UTC.
