Subtract dates and compare with value

2

I have two dates: DataAcesso and DataAtual of type DateTime . I have one more field called TempoAtualizacaoAutomatica of 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 this?

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 did not come to what I wanted.

As I'm doing this in MVC, this part evidently must be in Controllers , right?

Thank you!

To the models.

Dashboard

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 disabled.

There are two classes. Note that I will need to set items from one and the other for this method to work.

    
asked by anonymous 29.12.2014 / 12:47

2 answers

2

By all means, yes you should put in Controller . I do not guarantee because the question does not give many details. Without telling you where to apply this or putting relevant parts of the application it is even difficult to answer the main part of the question. I do not even understand what "on" and "off" means in your case. I would say this is what you want:

public bool EstaOn(LogAcesso acesso) {
    return (acesso.DataAtual - acesso.DataAcesso).TotalDays < painel.TempoAtualizacaoAutomatica;
}

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

    
29.12.2014 / 13:04
0

From what I understand, everything you said would be something like this:

TimeSpan res = DataAtual - DataAcesso;
if ( (byte)res.TotalHours < TempoAtualizacaoAutomatica )
{
    //on
}
else
{
    //off
}   
    
16.09.2015 / 03:22