How do I calculate with 3 variables of type DateTime?

2

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 make this calculation?

    
asked by anonymous 14.10.2015 / 03:21

1 answer

5

So:

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

See running on dotNetFiddle .

You can say that I changed the type. But now it's all right. DateTime marks a point in time, does not 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 type to save a time interval with TimeSpan .

We can only do the right thing. You'd even have to make the account using the wrong way, but when you conceptualize wrong, sooner or later you'll have problems.

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

    
14.10.2015 / 03:38