3

I have a timestamp with the value 634876488000000000 and the date expected is 04th November 2012, 18:00:00. When I convert it though, the year is 3612 instead of 2012 (the day, month and time are all correct).

Here is an example:

Console.WriteLine(DateTime.FromFileTimeUtc(634876488000000000).ToString());
Console.WriteLine(DateTime.FromFileTime(634876488000000000).ToString());

And the output:

04/11/3612 18:00:00
04/11/3612 18:00:00

Upon further research I discovered that if I use Get-Date 634876488000000000 in Powershell, I get the correct, expected date of 04 November 2012 18:00:00.

Could anyone explain how to correctly convert the time stamp to a .NET DateTime object using C# please?

default
  • 11,485
  • 9
  • 66
  • 102
Neil Mountford
  • 1,951
  • 3
  • 21
  • 31

3 Answers3

6

My first guess was that the number is DateTime.Ticks, just tried the following in Visual studio and I get. {04/11/2012 6:00:00 PM} for:

DateTime dt = DateTime.MinValue.AddTicks(634876488000000000);

Or use the DateTime Constructor which takes ticks as parameter.

DateTime dt = new DateTime(634876488000000000);
Habib
  • 219,104
  • 29
  • 407
  • 436
4

A proper Windows FILETIME as defined in the API is the number of ticks since January 1, 1601 at midnight UTC. That is, 1601-01-01T00:00:00Z. That doesn't appear to be what you've got. That's why you're off by 1601 years.

You appear to have the number of ticks since January 1, 0001 at midnight. That's all Get-Date is using, and that's what the single integer constructor for DateTime uses.

DateTime ntepoch = new DateTime(1601,1,1,0,0,0,DateTimeKind.Utc); // Ticks is 504911232000000000
Console.WriteLine(DateTime.FromFileTimeUtc(634876488000000000 - ntepoch.Ticks));
Console.WriteLine(DateTime.FromFileTime(634876488000000000 - ntepoch.Ticks));
Console.WriteLine(new DateTime(634876488000000000)));

For me, in timezone GMT-5, this outputs:

11/4/2012 6:00:00 PM
11/4/2012 1:00:00 PM
11/4/2012 6:00:00 PM
Bacon Bits
  • 30,782
  • 5
  • 59
  • 66
0

You can use DateTime(long ticks) constructor to get the correct date:

Console.WriteLine((new DateTime(634876488000000000)).ToString());
John Gathogo
  • 4,495
  • 3
  • 32
  • 48