I have a field of type TimeStamp in database, which is converted in byte[] in c# code, and i need to convert it to DateTime value.
So i want to convert from an array of bytes into DateTime.
Already used this code:
byte[] byteValue = someValue;
long longVar = BitConverter.ToInt64(byteValue);
DateTime dateTimeVar = DateTime.FromBinary(longVar);
is this ok?
No, that is not correct.
The
FromBinarymethod takes a long value that is created using theToBinarymethod. It contains theKindandTickscomponents, and this is not what a database timestamp contains.Using
BitConverterto get the long value is correct, but then you have to take the time origin for the time stamp and add the long value as the correct unit. Assuming it’s a timestamp from a MySQL database, IIRC it’s the number of milliseconds from 1980-01-01: