1

How can I go about converting a date to a long? Casting it doesn't work :/.

Levi H
  • 3,426
  • 7
  • 30
  • 43

4 Answers4

7

Every DateTime structure has a method called ToBinary() and a method called FromBinary() that according to MSDN

Use the ToBinary method to convert the value of the current DateTime object to a binary value. Subsequently, use the binary value and the FromBinary method to recreate the original DateTime object.

The return value from ToBinary() is a long value, and the input value for FromBinary is again a long value.

Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
Steve
  • 213,761
  • 22
  • 232
  • 286
2

You can get the number of ticks since DateTime.MinValue which is represented by the DateTime object. You can then, when you load it from file, you can recreate the DateTime object using the constructor which takes a number of ticks. For instance:

Dim ticks As Long = myDate.Ticks
Dim myDate2 As Date = New Date(ticks)
Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
  • +1 Ticks works as well, but perhaps the shown constructor doesn't take account of timezone. Really never have to worry about timezones. – Steve Oct 02 '12 at 13:52
1

Well In Java, we might do something like this :

            String str_date="11-June-07";
            DateFormat formatter ; 
            Date date ; 
            formatter = new SimpleDateFormat("dd-MMM-yy");
            date = (Date)formatter.parse(str_date); 
            long longDate=date.getTime();
            System.out.println("Today is " +longDate );

Try this in VB.net :

      Dim d As Date = Today
       MsgBox(d.ToOADate)
venkatKA
  • 2,399
  • 1
  • 18
  • 22
0

You can use the Ticks property, assuming that's the Long value you need:

http://msdn.microsoft.com/en-us/library/system.datetime.ticks.aspx

See also:

Format from ticks to date

Community
  • 1
  • 1
mcknz
  • 467
  • 9
  • 27