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
DateTimeOffset.FromFileTime() Method in C#
The DateTimeOffset.FromFileTime() method in C# converts a Windows file time value (in ticks) to an equivalent DateTimeOffset representing the local time. This method is useful when working with file timestamps or Windows-specific time representations.
Syntax
Following is the syntax for the DateTimeOffset.FromFileTime() method −
public static DateTimeOffset FromFileTime(long fileTime);
Parameters
fileTime: A Windows file time expressed in ticks. This represents the number of 100-nanosecond intervals that have elapsed since 12:00 midnight, January 1, 1601 A.D. (C.E.) Coordinated Universal Time (UTC).
Return Value
Returns a DateTimeOffset object that represents the date and time equivalent of the Windows file time, adjusted to the local system's time zone.
Using FromFileTime() with Zero Value
When passing zero as the file time, it represents the Windows file time epoch (January 1, 1601) −
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("F"));
}
}
The output of the above code is −
DateTimeOffset = 01 January 1601, 12:00:00 Full format: Monday, January 1, 1601 12:00:00 AM
Using FromFileTime() with File Time Ticks
Here's an example using a larger file time value to demonstrate the conversion −
using System;
public class Demo {
public static void Main() {
DateTimeOffset offset1 = DateTimeOffset.FromFileTime(200000000);
Console.WriteLine("File time 200000000: {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss}", offset1);
DateTimeOffset offset2 = DateTimeOffset.FromFileTime(132000000000000000);
Console.WriteLine("File time 132000000000000000: {0:dd} {0:MMMM} {0:yyyy}, {0:HH}:{0:mm}:{0:ss}", offset2);
}
}
The output of the above code is −
File time 200000000: 01 January 1601, 12:00:20 File time 132000000000000000: 13 May 2019, 16:00:00
Working with Current File Time
This example demonstrates converting the current time to file time and back to DateTimeOffset −
using System;
public class Demo {
public static void Main() {
DateTime now = DateTime.Now;
long fileTime = now.ToFileTime();
DateTimeOffset fromFileTime = DateTimeOffset.FromFileTime(fileTime);
Console.WriteLine("Original DateTime: " + now);
Console.WriteLine("File Time (ticks): " + fileTime);
Console.WriteLine("Converted back: " + fromFileTime);
}
}
The output of the above code is −
Original DateTime: 12/15/2023 3:45:30 PM File Time (ticks): 133470267300000000 Converted back: 12/15/2023 3:45:30 PM -05:00
Conclusion
The DateTimeOffset.FromFileTime() method provides a convenient way to convert Windows file time values to DateTimeOffset objects. It automatically adjusts for the local time zone and handles the conversion from the Windows epoch (January 1, 1601) to modern date representations.
