Why does Convert.ToDateTime (Int64) fail?

Why the following happens: "Invalid rollback from" Int64 "to" DateTime "". an exception?

long oldDate=new DateTime(2015, 1, 1).Ticks; DateTime newDate=Convert.ToDateTime(oldDate); 

.Ticks is long / Int64, and MSDN documents Convert.ToDateTime (Int64) show a method that accepts long / Int64.

 public static DateTime ToDateTime( long value ) 

EDIT: As stated in ebyrob below, this should be:

 long oldDate=new DateTime(2015, 1, 1).Ticks; DateTime newDate=new DateTime(oldDate); 
+6
source share
2 answers

In the MSDN documentation on the Convert.ToDateTime Method (Int64) :

A call to this method always raises an InvalidCastException.

and

Return Value Type: System.DateTime This conversion is not supported. The value is not returned.

Source: http://msdn.microsoft.com/en-us/library/400f25sk.aspx (the link points to .NET 4.5 documentation, but it is the same for all versions prior to 2.0).

I'm not sure why this is not supported, especially if new DateTime(oldDate) works well:

 DateTime newDate = new DateTime(oldDate); 
+6
source

DateTime.FromBinary(oldDate) works

0
source

Source: https://habr.com/ru/post/957086/


All Articles