63

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
  • 98,240
  • 88
  • 296
  • 433
Tono Nam
  • 34,064
  • 78
  • 298
  • 470
  • 12
    @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

9 Answers9

125

To long from DateTime:

long DateTime.Ticks

To DateTime from long:

new DateTime(long)

abatishchev
  • 98,240
  • 88
  • 296
  • 433
15

From long to DateTime: new DateTime(long ticks)

From DateTime to long: DateTime.Ticks

Chuck Conway
  • 16,287
  • 11
  • 58
  • 101
Jamie
  • 3,901
  • 3
  • 23
  • 27
11

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

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
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
  • 16,319
  • 7
  • 50
  • 82
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
  • 35,726
  • 5
  • 62
  • 78
1
   long dateTime = DateTime.Now.Ticks;
   Console.WriteLine(dateTime);
   Console.WriteLine(new DateTime(dateTime));
   Console.ReadKey();
Jaydeep Shil
  • 1,894
  • 22
  • 21
1

There is a DateTime constructor that takes a long.

DateTime today = new DateTime(t); // where t represents long format of dateTime 
Dave New
  • 38,496
  • 59
  • 215
  • 394
Adam Lamer
  • 104
  • 4
1

If you want to use seconds since 1970 instead of Ticks:

UTC:

long secondsSince1970 = DateTimeOffset.Now.ToUnixTimeSeconds();

Local time:

long secondsSince1970 = DateTime.Now.Ticks / 10000000 - 62135596800;

And back to DateTime

DateTime value = DateTime.MinValue.AddSeconds(secondsSince1970).AddYears(1969);
Michael Santos
  • 466
  • 7
  • 15
0

I will add some additional conversion from string to DateTime to long

string timeString = "2016-08-04";
DateTime date = DateTime.Parse(timeString);
long dateTime = date.Ticks;

And here is the shorthand sort of speak:

long dateTime = DateTime.Parse("2016-08-04").Ticks;

//And for the long to DateTime

DateTime date = new DateTime(dateTime);
Zahari Kitanov
  • 510
  • 7
  • 15
  • This doesn't answer the question. Please review the question. – Enigmativity Dec 25 '22 at 23:58
  • @Enigmativity Is that edit enough? – Zahari Kitanov Dec 26 '22 at 00:26
  • That certainly now covers off on the key points in the question. The two criticisms I have are that you've introduced parsing for no good reason - the OP didn't ask about parsing - and you've not really added any compelling new information than the existing answers offer. – Enigmativity Dec 26 '22 at 00:31
  • Thanks for the feedback, it was just some extra stuff that I am dealing right now and thought to share my additional experience. I will try to keep on point for future answers. – Zahari Kitanov Dec 26 '22 at 00:36
  • 1
    Then say why you've added it. Give it some context as to why you think this is a valuable addition. – Enigmativity Dec 26 '22 at 00:44