gpt4 book ai didi

c# - DateTime 时间差和毫秒数

转载 作者:太空宇宙 更新时间:2023-11-03 22:04:53 31 4
gpt4 key购买 nike

我需要将两个日期时间的差异精确到毫秒比较,第一个日期时间将小于第二个,以停止 gridviews 删除事件中的循环

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
if (!Ok2Delete(e.RowIndex)) return;

// your logic goes here and the above IF statement
// will hopefully guarantee your code to run once.
}
private bool Ok2Delete(int ri) // ri is the record index to be deleted
{
if (Session["ri"] == null ||
(!((ri == ((int)Session["ri"])) &&
(DateTime.Now.Subtract((DateTime)Session["ri_time_stamp"]).Seconds < 2))))
{
Session["ri"] = ri;
Session["ri_time_stamp"] = DateTime.Now;
return true;
}
return false;
}

此代码未按预期工作

最佳答案

使用 .TotalSeconds...否则 122 TotalSeconds 的 TimeSpan 将是 2 秒。

private bool Ok2Delete(int ri) // ri is the record index to be deleted
{
if (Session["ri"] == null ||
(!((ri == ((int)Session["ri"])) &&
(DateTime.Now.Subtract((DateTime)Session["ri_time_stamp"]).TotalSeconds < 2))))
{
Session["ri"] = ri;
Session["ri_time_stamp"] = DateTime.Now;
return true;
}
return false;
}

关于c# - DateTime 时间差和毫秒数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8798360/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com