To calculate the difference just make a subtraction, like this:
public TimeSpan TempoPermanencia() => HoraSaida - HoraEntrada;
This will produce a TimeSpan
which is exactly what you need. DateTime
, Contrary to what many people think is just a point in time, nothing else, so an input or output moment is a point in time. A period, an amount of time spent on something is represented by a TimeSpan
. Read his documentation to learn how to use it, take the stored time of the various possible.
The ideal would be not to consider only the hour and the minute. Can’t it be that the exit occurs the next day? Is there not the slightest chance of this occurring? Does it make such a difference to disregard seconds? If seconds are not important, wouldn’t it be interesting if they were discarded in the data entry? It would even be possible to do this on the property, but it would have to use clear criteria. Without certain requirements, no code will be produced right.
I made a more "complete" solution that I do not guarantee that does exactly what you want, lacking criteria to solve certain situations, for example, in the test done can give 1 hour and 43 minutes, or 1 hour and 44 minutes, It works, but there is no clear criterion as to whether this is what should happen. I didn’t even test it. Messing with date and time is a lot more complicated than it looks. I cut out the superfluous information on the property, but I could have stored it all and made that cut account only at the time of calculation.
using System;
using static System.Console;
public class Program {
public static void Main() {
var objeto = new AlgumaClasse();
objeto.HoraEntrada = DateTime.Now;
objeto.HoraSaida = DateTime.Now.AddHours(1).AddMinutes(43).AddSeconds(22);
WriteLine($"Permaneceu {(objeto.TempoPermanencia().ToString(@"hh\:mm"))}");
}
}
class AlgumaClasse {
public string Placa { get; set; }
public string Modelo { get; set; }
private DateTime horaEntrada;
public DateTime HoraEntrada {
get => horaEntrada;
set {
var tempo = default(DateTime).Add(value.TimeOfDay); //corta data
horaEntrada = tempo.AddSeconds(-tempo.Second); //corta segundos
}
}
private DateTime horaSaida;
public DateTime HoraSaida {
get=> horaSaida;
set {
var tempo = default(DateTime).Add(value.TimeOfDay);
horaSaida = tempo.AddSeconds(-tempo.Second);
}
}
public TimeSpan TempoPermanencia() => HoraSaida - HoraEntrada;
}
Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.
Explain better what you want. Set clear criteria. Should you take what? Do? What result do you want? Put where? Does this code indicate anything? What part of it is useful for the problem?
– Maniero
I will take time and timeSide and do the following: take the hours of timeSide and subtract with the hours of timeSbetween then the same thing with the minutes, so that I return another Datetime with the hours and minutes that the person was in place, put in the Tempopermanencia method, indicates yes, the time a person was parked, I would also like to know how to declare so that a Datetime only accept formats in hours and minutes, grateful.
– Caio Vieira
It has already changed in relation to what is described in the question.
DateTime
does not serve for it. I will try to do something useful. How date and time is input? Are you sure you’ll discard the other information than time and minute?– Maniero
Yes, just hour and minute
– Caio Vieira
They are tickets yes sir tbm.
– Caio Vieira
There won’t be no date
– Caio Vieira
He’s getting more and more confused.
– Maniero
Let me explain it to you
– Caio Vieira
The idea is for the user to type in a textbox the hourEntered and in another the hourAs the case where these 2 variables should have the format "HH:mm" hours and minutes, only, after that by the get and set method there would assign the variables that I mentioned above, after that I created the Temp method where the intention is to subtract the hours and minutes of these variables in order to be able to display the time that the person was parked on the computer screen, understood ?
– Caio Vieira
It would be better to [Dit] the question and consolidate all this in a unique way. I’m making an answer, but some points I don’t know how to resolve because I don’t have the requirement.
– Maniero