I would like to do a c# app which it does a if logical operation to verify if the time coincides with business hours. I tried to use this code below, but it show me an error
DateTime data = DateTime.Parse(txt_Hora.Text, System.Globalization.CultureInfo.CurrentCulture);
if (data > "09:00")
{
MySqlCommand cmd1 = new MySqlCommand("INSERT INTO produtos_pedidos (quantidade, data_pedido, subtotal, hora) VALUES ('" cmb_Quantidade.Text "','" txt_Data.Text "','" lbl_Subtotal.Text "','" txt_Hora.Text "')", bdcon);
cmd1.ExecuteNonQuery();
MessageBox.Show("Reservado com sucesso");
}
Here's the error: The operator ">" can't be apply to DateTime type and string type
CodePudding user response:
Use time span for comparison
TimeSpan timeSpan = new TimeSpan(
0,
data.Hour,
data.Minute,
data.Second,
data.Millisecond);
var ttt = timeSpan.ToString();
TimeSpan timeSpan2 = new TimeSpan(0, 9, 0, 0, 0); // "09:00"
if (timeSpan > timeSpan2)
{
... //Your code
}
CodePudding user response:
Just compare a TimeSpan
with a TimeSpan
if (data.TimeOfDay > new TimeSpan(9,0,0))
...