加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 将DateTime与存储在数据库中的日期进行比较

发布时间:2020-12-16 01:41:59 所属栏目:百科 来源:网络整理
导读:我有两个约会时间. 我从SQL服务器获取一个日期时间,另一个日期是客户端的日期时间. 两个日期时间相等但在我的代码中返回不相等. 为什么? var mngProduct = new ProductManager();var file = mngProduct.GetProductImageData(int.Parse(context.Request["ima
我有两个约会时间.
我从SQL服务器获取一个日期时间,另一个日期是客户端的日期时间.

两个日期时间相等但在我的代码中返回不相等.
为什么?

var mngProduct = new ProductManager();
var file = mngProduct.GetProductImageData(int.Parse(context.Request["imageId"]),imageSize);

if (!String.IsNullOrEmpty(context.Request.Headers["If-Modified-Since"]))
{
  System.Globalization.CultureInfo provider = System.Globalization.CultureInfo.InvariantCulture;
  var lastMod = DateTime.ParseExact(context.Request.Headers["If-Modified-Since"],"r",provider).ToLocalTime();
  if (lastMod==file.CreatedOn)//return false always
  {
    res.StatusCode = 304;
    res.StatusDescription = "Not Modified";
    return;
  }
}
res.ContentType = file.MimeType;
res.AddHeader("Content-disposition","attachment; filename=" + file.FileName);
res.AddHeader("Content-Length",file.Content.Length.ToString());
res.BinaryWrite(file.Content.ToArray());
res.Cache.SetCacheability(HttpCacheability.Public);
res.Cache.SetLastModified(file.CreatedOn);

解决方法

当达到毫秒时,SQL DateTime的精度较低.我会检查两个日期的差异是否小于一个小的TimeSpan.

if(Math.Abs(date1.Subtract(date2).TotalSeconds)>1) // difference of more than 1 second
{
     ...
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读