Subtract dates and compare with value

Asked

Viewed 2,457 times

2

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

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

Condition is: If "dates" are less than TempoAtualizacaoAutomatica "On", otherwise "Off".

How do I do that?

I’ve tried the first part of that rule like this:

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

But you didn’t get what I wanted.

As I’m doing this at MVC, this part should evidently be on Controllers, right?

Thank you!

To models.

Painelchamada

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; }

Painelchamada 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 of one and the other for this method to work.

  • What is the condition to be met? What is the rule for variable Tempoatualizacaoautomatica be "ON" or "OFF"?

  • If the comparison of dates is less than Tmepoupdatedautomatica. Example: Subtracting the dates I got 5 I will compare with Timetimethe Utomatica of my Call and if it is smaller will be On

  • This is important: of what type is the variable and what measure do you represent in Tempoatualizaoautomatica? Days, hours, minutes, a TimeSpan?

  • But Tempoatualizaoautomatica is not byte type?

  • Exactly a Timespan... The code was generated automatically and Tempoatualizacaoautomatica is a byte and for the use case itself it is necessary to respect this

  • TempoAtualizacaoAutomatica represents what? Days, hours, minutes, seconds or some other unit of time?

  • Represents Hours

  • @Take a look at [tour]. You can accept an answer if it solved your problem. You can vote for all the posts on the site as well. Did any help you more? You need something to be improved?

  • @I don’t understand why you TempoAtualizacaoAutomatica is with type 'byte' need to know this to know how to convert this byte to a value (integer, double, or timeSpan) to create an algorithm that suits you.

Show 4 more comments

2 answers

2

It seems, yes you should put in Controller. I can’t guarantee because the question doesn’t go into too much detail. Without telling you where you will apply this or putting 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 that’s what you want:

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

I put in the Github for future reference.

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

  • See if you can understand with the edits made.

  • It doesn’t help, because these data can infer what they are, but their context is not yet known. What is difficult to understand is why you need this, where you need it. How this information is arranged in your application. Loose data does not help. My answer is "loose", it has no context, but it is consistent with the question where everything is loose, without context. I understood from the beginning that "on" and "off" indicate whether pianel is activated or not, but what this means in the code?

  • Well, I don’t know if I can define it right. But that this condition is nothing more than to show in my Call Panel if the call is active, it’s like a flag. It is a monitoring that uses the Logacesso and Painelchamada, the two talk, this method should be in Painelchamada and analyzing well maybe in the class itself and not in a Controller. Because in the View that displays this status has as model Painelchamada.

  • If you can’t define what you want right, I can’t define a precise answer. What I put in does what it looks like you want. But where to put this I do not know, I have no context. From what you are talking I believe that it is in the controller call panel but exactly how, I don’t know, I don’t have enough information. I’ve changed something, but it’s kick.

0

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.