如何从两个日期时间中减去两次并计算经过时间是否为 61 秒?
How to subtract two times from two datetime and calculate is elapse time from 61 seconds or not?
我正在处理 asp.net 核心 MVC 项目。这个关于识别在线和离线用户的项目,我有两个日期时间,一个存储在数据库中,另一个是当前日期时间,我必须知道存储在数据库中的时间是否从 61 秒开始?
我减去两个 Datetime 最后使用 TotalSeconds property.but 我的输出是 -22095 或 2319208 等等。
public void CheckUserStatus()
{
DateTime now = DateTime.Now;
var userTime = _context.Sessions.Where(x => x.LastOnline).Select(x => new {x.LastConnectTime, x.Id});
foreach (var time in userTime)
{
TimeSpan diffrence = now.Subtract(time.LastConnectTime);
int mytime = Convert.ToInt32(diffrence.TotalSeconds);
if ( mytime < 61)
{
Console.WriteLine(time.Id);
}
}
}
我预计时间会以秒为单位,例如,现在我的输出是-22095或2319208,依此类推,但我不知道2319208是不是正常时间?
您可以像这样轻松检查:
DateTime now = DateTime.Now;
TimeSpan past = now - now.Subtract(TimeSpan.FromSeconds(60));
TimeSpan post = now - now.Subtract(TimeSpan.FromSeconds(61));
Console.WriteLine(now);
// Should be False: Passed time is less than 60 seconds
Console.WriteLine(past.TotalSeconds > 60);
// Should be True: Passed time is more than 60 seconds
Console.WriteLine(post.TotalSeconds > 60);
我正在处理 asp.net 核心 MVC 项目。这个关于识别在线和离线用户的项目,我有两个日期时间,一个存储在数据库中,另一个是当前日期时间,我必须知道存储在数据库中的时间是否从 61 秒开始?
我减去两个 Datetime 最后使用 TotalSeconds property.but 我的输出是 -22095 或 2319208 等等。
public void CheckUserStatus()
{
DateTime now = DateTime.Now;
var userTime = _context.Sessions.Where(x => x.LastOnline).Select(x => new {x.LastConnectTime, x.Id});
foreach (var time in userTime)
{
TimeSpan diffrence = now.Subtract(time.LastConnectTime);
int mytime = Convert.ToInt32(diffrence.TotalSeconds);
if ( mytime < 61)
{
Console.WriteLine(time.Id);
}
}
}
我预计时间会以秒为单位,例如,现在我的输出是-22095或2319208,依此类推,但我不知道2319208是不是正常时间?
您可以像这样轻松检查:
DateTime now = DateTime.Now;
TimeSpan past = now - now.Subtract(TimeSpan.FromSeconds(60));
TimeSpan post = now - now.Subtract(TimeSpan.FromSeconds(61));
Console.WriteLine(now);
// Should be False: Passed time is less than 60 seconds
Console.WriteLine(past.TotalSeconds > 60);
// Should be True: Passed time is more than 60 seconds
Console.WriteLine(post.TotalSeconds > 60);