Hello people i am doing an app on C# and now i am with a doubt.
I need to represent relative time, for example i have 02:30:00 i will need to say that is 2,5 hours.
How can i do that, and i need that have to be with 2 decimals here is what i have done so far, but i gives me errors with timespan
private string Horas_Matutinas(string cedula, DateTime desde, DateTime hasta)
{
string respuesta = "";
DateTime resto = Convert.ToDateTime("00:00:00");
//Llamo la conexion SQL
SqlConnection Wdcon_usuario = new SqlConnection(WDcon);
SqlCommand usuario = new SqlCommand();
SqlDataReader usuarioDR = null;
TimeSpan tiempo = Convert.ToDateTime("00:00:00") - Convert.ToDateTime("00:00:00");
//Instancio la conexion SQL
usuario.Connection = Wdcon_usuario;
//Registro el Query SQL
usuario.CommandText = "SELECT * FROM matutino WHERE " +
"(cedula = @ID) AND " +
"(desde >= @DESDE) AND " +
"(hasta <= @HASTA)";
usuario.Parameters.AddWithValue("@ID", Convert.ToInt64(cedula));
usuario.Parameters.AddWithValue("@DESDE", Convert.ToDateTime(desde));
usuario.Parameters.AddWithValue("@HASTA", Convert.ToDateTime(hasta));
//Abro la conexion
Wdcon_usuario.Open();
//Ejecuto la consulta
usuarioDR = usuario.ExecuteReader();
//Empiezo el ciclo
while (usuarioDR.Read())
{
TimeSpan tiempX = (DateTime)usuarioDR["tiempotrbajado"] - resto;
tiempo = tiempo + tiempX;
Double tiemp = Convert.ToDouble(tiempo);
respuesta = tiempo.ToString("0.00");
}
//Cierro la conexion
Wdcon_usuario.Close();
//Termino la sentencia SQL
//int i = 0;
int total = 8;
int caracteres = respuesta.Length;
int restantes = total - caracteres;
//respuesta.PadLeft(restantes, "0");
string s = new String('0', restantes) + respuesta;
return s;
}
It looks like you need the difference between two times here, this example might help you out.
Also, you can create
restoandtiempowithout usingConvert. Instead, you can use: