How can I convert an array of bytes into DateTime
? I must do this because I use a property (named CreationDataStamp
) with attribute Timestamp
for entity framework. A second property (named CreationDate
) I use for convert the byte array into DateTime
.
[System.ComponentModel.DataAnnotations.Timestamp]
public byte[] CreationDateStamp { get; set; }
[System.ComponentModel.DataAnnotations.Schema.NotMapped]
public DateTime CreationDate
{
get
{
return Convert.ToDateTime(CreationDateStamp);
}
}
The problem is that I've this exception on the 9 th line:
Unable to cast object of type
System.Byte[]
to typeSystem.IConvertible
.
Update:
I've also tried this from this question: How to convert a byte[] into datetime in C#?
long longVar = BitConverter.ToInt64(CreationDateStamp, 0);
DateTime dateTimeVar = new DateTime(1980,1,1).AddMilliseconds(longVar);
but I've got this exception:
Value to add was out of range.
Parameter name: value
Can you help me?
Sql server Timestamp
is not a DateTime
. It is a versioning column for the tuple (row).
In short, you can't do what you are trying to do.
Try something like this:
long longVar = BitConverter.ToInt64(byteValue, 0);
DateTime dateTimeVar = new DateTime(1980,1,1).AddMilliseconds(longVar);
Hope that helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.