How can I go about converting a date to a long? Casting it doesn't work :/.
4 Answers
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.

- 43,358
- 8
- 68
- 105

- 213,761
- 22
- 232
- 286
-
3Nice! Didn't know about that one. – Steven Doggart Oct 01 '12 at 14:50
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)

- 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
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)

- 2,399
- 1
- 18
- 22
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: