How to calculate with 3 variables of type Datetime?

Asked

Viewed 269 times

4

I have these 4 variables:

public System.DateTime TempoOtimista { get; set; }
public System.DateTime TempoProvavel { get; set; }
public System.DateTime TempoPessimista { get; set; }
public System.DateTime TempoRevisado { get; set; }

TempoRevisado =  (TempoOtimista  + TempoProvavel + TempoPessimista) / 3;

How do I calculate?

  • Solved the problem? Need more explanation?

  • @bigown You were very clear, but in my case the best option would be the use of type Datetime . My project looks like this: var otimista = (atividade.TempoOtimista - atividade.Inicio).TotalDays;
 var provavel = (atividade.TempoProvavel - atividade.Inicio).TotalDays;
 var pessimista = (atividade.TempoPessimista - atividade.Inicio).TotalDays;

  • You can keep using it, but it’s not the best option, it’s wrong. And what you did does not solve what you described in the question. You’re talking about something completely different than what was described.

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

1 answer

8

Thus:

public TimeSpan TempoOtimista { get; set; }
public TimeSpan TempoProvavel { get; set; }
public TimeSpan TempoPessimista { get; set; }
public TimeSpan TempoRevisado { get; set; }

TempoRevisado = new TimeSpan(0, 0,
                (int)(TempoOtimista + TempoProvavel + TempoPessimista).TotalSeconds / 3);

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

You can say I changed the kind. But now you’re right. DateTime score a point in time, don’t mark a time spent. This is completely wrong. With wrong data, you can only get wrong results. So the first thing to fix is to change the guy to save a time interval with TimeSpan.

We can only do it the right way. You could do the math using the wrong way, but when you conceptualize yourself wrong, sooner or later you’ll have problems.

At some point I had to take the amount of seconds because the TimeSpan does not allow to divide.

Browser other questions tagged

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