I have a record in my database which saves the date and time in this format, the field is Datetime
:
2020-03-04 15:12:08.540
Now, from C# I get that field using a query
and assign it to a variable of type DateTime
.
I have declared another variable to obtain the datetime
current one to compare it with the one in the database and thus obtain the elapsed difference in minutes of the last record against the new record that I am trying to make.
DateTime CompFch;
DateTime fchNow = DateTime.Now;//fecha y hora actual
if (reader.Read())
{
CompFch = reader.GetDateTime(0);//obtengo y asigno la fecha de la dB a mi variable
TimeSpan result = fchNow.Subtract(CompFch).Duration();
string val = Convert.ToString(result.Minutes);//aqui obtengo los minutos transcurridos
if (Convert.ToInt32(val) <= 5)//
{
return "Ya está registrado.";
}
else
{
return "";
}
}
else
{
return "";
}
For example, I have that registration date, but I compare it with today's date and it only tells me that at most 15 min have elapsed, I mean that's wrong, it should be many more minutes because a day has already passed.
My idea is if it is already registered and if I tried to register again at the same moment, but 5 minutes have not elapsed since its first registration, then it returns "Already registered."
You must spend at least 5 minutes before you can re-register.
You are checking the property
TimeSpan.Minutes
:To check the total minutes of a
TimeSpan
you must check the propertyTimeSpan.TotalMinutes
: