[英]Calculation did not perform to calculate the difference in total days between 2 dates in c#asp.net sql linq
DateTime currentDate = DateTime.Now;
var query = (from m in db.Users where m.EmailAddress == emailAddress select m.Last_Login_Date).Single();
DateTime lastLoginDate = Convert.ToDateTime(query);
double diffDays = (currentDate - lastLoginDate).TotalDays;
if (diffDays < 72) {
Security.LoginUser(u.Name, u.Role, rememberMe);
u.Last_Login_Date = currentDate;
} else {
Response.Redirect("ResetPwd.aspx");
}
我試圖執行計算以找出總天數的差異。 因此,如果差異大於 72 天,他們將被迫進行重置密碼。 但現在公式無法執行。 我認為我的代碼有一些錯誤。 所以 currentDate 和 db.Users Last_Login_Date 是 DateTime 類型。 任何人都知道如何進行計算,還是應該將我的 UserDB Last_Login_Date 更改為字符串類型?
請嘗試以下操作:
//DateTime.Subtract() method will return a 'TimeSpan' value.
var timeSpan = currentDate.Subtract(lastLoginDate);
var diffDays = timeSpan.Days;
if(diffDays < 72)
{
//Code here.
}
工作解決方案在以下鏈接中:
所以,我找到了我的問題的解決方案。 我改為 TimeSpan,現在一切正常。感謝您的貢獻。
DateTime currentDate = DateTime.Now;
var query = (from m in db.Users
where m.EmailAddress == emailAddress
select m.Last_Login_Date).Single();
DateTime lastLoginDate = Convert.ToDateTime(query);
TimeSpan diffDays = currentDate.Subtract(lastLoginDate);
if (diffDays.TotalDays> 72)
{
Response.Redirect("ResetPwd.aspx");
}
else
{
Security.LoginUser(u.Name, u.Role, rememberMe);
u.Last_Login_Date = currentDate;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.