好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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

我有两个约会时间.

我从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
{
     ...
}

查看更多关于c# – 将DateTime与存储在数据库中的日期进行比较的详细内容...

  阅读:47次