奇怪的DateTime.ToBinary()结果?

时间:2017-03-30 15:44:37

标签: c#

我有以下代码:

var today = DateTime.Now;
var newToday = new DateTime(today.Year, today.Month, today.Day, 23, 59, 59);
Debug.WriteLine($"Today: {today} | Today binary: {today.ToBinary()}");
Debug.WriteLine($"NewToday: {newToday} | NewToday binary: {newToday.ToBinary()}");
DateTime startOfPeriod;
var startBinary = 0L;
var endBinary = 0L;
var endOfPeriod = new DateTime(today.Year, today.Month, today.Day, 23, 59, 59);
Debug.WriteLine($"EndOfPeriod: {endOfPeriod} | EndOfPeriod binary: {endOfPeriod.ToBinary()}");

startOfPeriod = endOfPeriod.Subtract(TimeSpan.FromDays(1));
startOfPeriod = startOfPeriod.Add(TimeSpan.FromSeconds(1));
startBinary = startOfPeriod.ToBinary();
endBinary = endOfPeriod.ToBinary();
Debug.WriteLine($"StartOfPeriod: {startOfPeriod} | StartOfPeriod Binary: {startBinary}");
Debug.WriteLine($"EndOfPeriod: {endOfPeriod} | EndOfPeriod Binary: {endBinary}");

输出是:

Today: 30.03.2017 17:39:32 | Today binary: -8587107185125548526
NewToday: 30.03.2017 23:59:59 | NewToday binary: 636265151990000000
EndOfPeriod: 30.03.2017 23:59:59 | EndOfPeriod binary: 636265151990000000
StartOfPeriod: 30.03.2017 00:00:00 | StartOfPeriod Binary: 636264288000000000
EndOfPeriod: 30.03.2017 23:59:59 | EndOfPeriod Binary: 636265151990000000

为什么today的二进制值(long)与newToday有很大不同???

我将日期序列化到Redis数据库,它们都是负数(存储为字符串)。但是当我如上所示进行计算时,我得到了那些奇怪的正值!如果你然后搜索数据库,那么将永远不会有任何匹配的记录!

有人能解释一下这里发生了什么吗?

0 个答案:

没有答案