60

I want to store dates as numbers in a table. I know how to do that but I don't know how to go back. How can I cast a long variable to ToDateTime.

DateTime now = DateTime.Now;
long t = now.ToFileTime();
DateTime today = t.ToDateTime;  // I am looking for something like this line. This Method does not exist

I know there are many ways of converting DateTime to long. I don't mind which technique to use. I just want to have a way where I can convert back and forth.

abatishchev
  • 95,331
  • 80
  • 293
  • 426
Tono Nam
  • 31,694
  • 75
  • 272
  • 444
  • 11
    @DourHighArch: I disagree. The question is precisely stated and the intent is clear. "Got a `DateTime`, how to reversibly convert it to a `long`?" This question can be and deserves to be answered. I had precisely this question, and got the answer right here thanks to the OP. There is nothing more frustrating than answers saying "Why do you want to do that? DBs can store dates and times." And the question gets unanswered. – Laurent LA RIZZA May 18 '16 at 09:24

7 Answers7

119

To long from DateTime:

long DateTime.Ticks

To DateTime from long:

new DateTime(long)

abatishchev
  • 95,331
  • 80
  • 293
  • 426
12

From long to DateTime: new DateTime(long ticks)

From DateTime to long: DateTime.Ticks

Chuck Conway
  • 16,041
  • 10
  • 58
  • 100
Jamie
  • 3,701
  • 3
  • 21
  • 26
11

use the pair long t = now.Ticks and DateTime Today = new DateTime(t)

Scott Chamberlain
  • 121,188
  • 31
  • 271
  • 414
5

Since you're using ToFileTime, you'll want to use FromFileTime to go the other way. But note:

Ordinarily, the FromFileTime method restores a DateTime value that was saved by the ToFileTime method. However, the two values may differ under the following conditions:

If the serialization and deserialization of the DateTime value occur in different time zones. For example, if a DateTime value with a time of 12:30 P.M. in the U.S. Eastern Time zone is serialized, and then deserialized in the U.S. Pacific Time zone, the original value of 12:30 P.M. is adjusted to 9:30 A.M. to reflect the difference between the two time zones.

If the DateTime value that is serialized represents an invalid time in the local time zone. In this case, the ToFileTime method adjusts the restored DateTime value so that it represents a valid time in the local time zone.

If you don't care which long representation of a DateTime is stored, you can use Ticks as others have suggested (Ticks is probably preferable, depending on your requirements, since the value returned by ToFileTime seems to be in the context of the Windows filesystem API).

Dan J
  • 15,852
  • 6
  • 49
  • 79
3

There are several possibilities (note that the those long values aren't the same as the Unix epoch.

For your example (to reverse ToFileTime()) just use DateTime.FromFileTime(t).

Mario
  • 34,518
  • 5
  • 56
  • 76
1
   long dateTime = DateTime.Now.Ticks;
   Console.WriteLine(dateTime);
   Console.WriteLine(new DateTime(dateTime));
   Console.ReadKey();
Jaydeep Shil
  • 1,674
  • 19
  • 20
1

There is a DateTime constructor that takes a long.

DateTime today = new DateTime(t); // where t represents long format of dateTime 
Dave New
  • 36,059
  • 54
  • 198
  • 383
Adam Lamer
  • 104
  • 4