codertocode
codertocode

Reputation: 41

Convert string to time c#

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

Upvotes: 0

Views: 173

Answers (5)

DotNet Developer
DotNet Developer

Reputation: 3018

Convert string to time c#

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
}

Or simply

if (data.TimeOfDay > new TimeSpan(0, 9, 0, 0, 0))
{
    ... //Your code
}

Or

if (data.TimeOfDay > TimeSpan.Parse("09:00"))
{
    ... //Your code
}

Upvotes: 1

Majid ALSarra
Majid ALSarra

Reputation: 394

1st you are comparing DateTime to string, so make a DateTime.Parse("09:00") as you did on the first line to have a datetime that can be compared to the data variable

2nd, to compare 2 DateTime variables use DateTime.Compare instead of > operation as the error massage advises.

Upvotes: 0

Gian Paolo
Gian Paolo

Reputation: 4249

Just work with two TimeSpan

TimeSpan timeOfTheDay = TimeSpan.Parse(txt_Hora.Text, System.Globalization.CultureInfo.CurrentCulture);
if (timeOfTheDay > new TimeSpan(9,0,0))
    ...

Upvotes: -2

Panagiotis Kanavos
Panagiotis Kanavos

Reputation: 131180

Don't use string concatenation to construct SQL statements. This exposes you to SQL Injection attacks and conversion errors like this one. Use parameterized queries instead :

var sql="INSERT INTO produtos_pedidos (quantidade, data_pedido, subtotal, hora) VALUES (@quant,@data,@total,@hora)";


using var cmd=new MySqlCommand(sql, bdcon);
cmd.Parameters.AddWithValue("@quant",quant);
cmd.Parameters.AddWithValue("@data",pedido);
cmd.Parameters.AddWithValue("@total",total);
cmd.Parameters.AddWithValue("@hora",hora);
cmd.ExecuteNonQuery();

Parse the text values into the proper type before using them. If possible, use UI elements that provide the type you want. If you use a combo with values or products, bind its value to the actual ID or quantity and use that value instead of the string label. If you want to pick a time, use a DateTimePicker set to work as a TimePicker, eg :

var quant=(int)cmb_Quantidade.SelectedValue;
var data=txt_Data.Text ;
var total=decimal.Parse(lbl_Subtotal.Text);
var hora=timePicker.Value.Time;

The time of day is represented by the TimeSpan type. The time of day of a DateTime is returned by the TimeOfDay property. If you can't use a DateTimePicker (why?) you need to parse the data into a TimeSpan:

var hora=TimeSpan.Parse(txt_Hora.Text);

Putting it all together :

var quant=(int)cmb_Quantidade.SelectedValue;
var data=txt_Data.Text;
var total=decimal.Parse(lbl_Subtotal.Text);
var hora=timePicker.Value.Time;

if (hora>TimeSpan.FromHours(9))
{
    using var cmd=new MySqlCommand(sql, bdcon);
    cmd.Parameters.AddWithValue("@quant",quant);
    cmd.Parameters.AddWithValue("@pedido",pedido);
    cmd.Parameters.AddWithValue("@total",total);
    cmd.Parameters.AddWithValue("@hora",hora);
    cmd.ExecuteNonQuery();
}

Upvotes: 2

user19255201
user19255201

Reputation: 61

You could use something like:

if (data.TimeOfDay > new TimeSpan(0, 9, 0, 0))
{
   // Whatever
}

Upvotes: 2

Related Questions