Subtract dates and compare with value

I have two dates: DataAcesso and DataAtual of type DateTime. I have one more field called TempoAtualizacaoAutomatica from typo byte.

I need to subtract these dates and compare the result with TempoAtualizacaoAutomatica.

The condition is: if " dates "is less than TempoAtualizacaoAutomatica" On", otherwise"Off".

How do I do that?

I've tried the first part of this rule like this:

TimeSpan final = Convert.ToDateTime("19/03/2007 12:30").Subtract(Convert.ToDateTime("19/03/2007 12:00"));

But it didn't get to what I wanted.

How am I doing this in MVC, this part evidently it must be in Controllers , right?

Thank you!

To models.

Panel call

public string Descricao { get; set; }
public bool FlagEstacaoI { get; set; }
public bool FlagEstacaoT { get; set; }
public bool FlagEstacaoD { get; set; }
public byte QtdeChamadasListadas { get; set; }
public byte QtdeChamadasFornecedor { get; set; }
public bool FlagExibirDesenhos { get; set; }
public string UrlChamadaAlerta { get; set; }
public byte TempoAtualizacaoAutomatica { get; set; }
public byte QtdeAcessosSimultaneos { get; set; }
public System.Guid Guid { get; set; }
public bool FlagBloqueioPainel { get; set; }

Panel Call Log Access

public int IdEmpresa { get; set; }
public int IdPainelChamada { get; set; }
public long Id { get; set; }
public System.DateTime DataAcesso { get; set;    }
public string Ip { get; set; }
public string CabecalhoHttp { get; set; }
public virtual PainelChamada PAINEL_CHAMADA { get; set; }

On Means: panel is working.

Off: panel is off.

Are two classes. Note that I will need to set items of one and the other for this method to work.

Author: Maniero, 2014-12-29

2 answers

To all indications, yes you should put in Controller. I do not guarantee because the question does not give much detail. Without you saying Where You are going to apply this or placing relevant parts of the application it is even difficult to answer the main part of the question. I don't even understand what "on" and "off" means in your case. I would say this is what you want:

public bool EstaOn(LogAcesso acesso) => (acesso.DataAtual - acesso.DataAcesso).TotalDays < painel.TempoAtualizacaoAutomatica;

I put on GitHub for future reference.

Can change the TotalDays by Days if you want to despise the fractional part. If you need other forms of rounding, you would have to define the rules for this and implement this algorithm.

 2
Author: Maniero, 2020-09-16 14:29:00

From what I understood of everything you said would be something like this:

TimeSpan res = DataAtual - DataAcesso;
if ( (byte)res.TotalHours < TempoAtualizacaoAutomatica )
{
    //on
}
else
{
    //off
}   
 0
Author: Christian Beregula, 2015-09-16 01:22:30