First of all Visual Studio cannot do something like this because it is an IDE, it is a tool for writing code and it is not its function to calculate dates. But if you want to know how to program in C# to do this, you can answer.
I will ignore the mistakes that would give other types of problem and nor compile.
Already an error in this code because nothing guarantees that the data written in the text field will be in the correct format, if it is not your code will break. This can be seen in some questions like this: Convert string to Datetime.
Is there any reason to use a TimeSpan
? Do you know what it’s for? Never use something you don’t know what it’s for and how to use it properly. If you want a time you shouldn’t use a TimeSpan
because it is about a elapsed time and not a time (a point in the timeline).
Since there is no time-only type on . standard NET (some use auxiliary libraries to handle it) you would have to use a DateTime
. But you can’t just consider hours anyway.
If you really want to use a precise time frame, it would be something like this:
using System;
using static System.Console;
public class Program {
public static void Main() {
var horaC1 = new DateTime(2019, 1, 1, 23, 0, 0);
var horaC2 = new DateTime(2019, 1, 2, 6, 0, 0);
var now = new DateTime(2019, 1, 2, 5, 0, 0);
if (now >= horaC1 && now <= horaC2 /*&& horarioC1Label1.Text != "00:00" && horarioC2Label1.Text != "00:00")*/) WriteLine("Turno 3");
now = new DateTime(2019, 1, 2, 7, 0, 0);
if (now >= horaC1 && now <= horaC2 /*&& horarioC1Label1.Text != "00:00" && horarioC2Label1.Text != "00:00")*/) WriteLine("Turno 3 ---");
}
}
I commented on the part that I don’t have access, I changed the data to be fixed and had it printed just to facilitate the test.
If you want to insist on using the time interval, you can do:
using System;
using static System.Console;
public class Program {
public static void Main() {
var horaC1 = new DateTime(2019, 1, 1, 23, 0, 0).TimeOfDay;
var horaC2 = new DateTime(2019, 1, 2, 6, 0, 0).TimeOfDay;
var now = new DateTime(2019, 1, 2, 5, 0, 0).TimeOfDay;
if (now >= horaC1 || now <= horaC2 /*&& horarioC1Label1.Text != "00:00" && horarioC2Label1.Text != "00:00")*/) WriteLine("Turno 3");
now = new DateTime(2019, 1, 2, 7, 0, 0).TimeOfDay;
if (now >= horaC1 || now <= horaC2 /*&& horarioC1Label1.Text != "00:00" && horarioC2Label1.Text != "00:00")*/) WriteLine("Turno 3 ---");
}
}
Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.
If you do this, you are guaranteeing both at the same time, within the crooked logic you want to use. In your logic the time should be between 23 and 6 hours. So if you pass 23 is within the range you want, and if you have not yet reached 6 is also.
If you really want to use the &&
you have to reverse the logic:
using System;
using static System.Console;
public class Program {
public static void Main() {
var horaC1 = new DateTime(2019, 1, 1, 23, 0, 0).TimeOfDay;
var horaC2 = new DateTime(2019, 1, 2, 6, 0, 0).TimeOfDay;
var now = new DateTime(2019, 1, 2, 5, 0, 0).TimeOfDay;
if (!(now < horaC1 && now > horaC2) /*&& horarioC1Label1.Text != "00:00" && horarioC2Label1.Text != "00:00")*/) WriteLine("Turno 3");
now = new DateTime(2019, 1, 2, 7, 0, 0).TimeOfDay;
if (!(now < horaC1 && now > horaC2) /*&& horarioC1Label1.Text != "00:00" && horarioC2Label1.Text != "00:00")*/) WriteLine("Turno 3 ---");
}
}
Much worse, right?
Note that all give the same result, what you want.
yes I know I have to use the dates together but it turns out that the dates change as the hours go by
– 568lu
The confilt is not in condition but in the Types you are using... What is
hora_now
??? if you leave your variables with a more descriptive name or explain your logic better and what you are trying to do we can help more– Leandro Angelo
@568lu Have any of the answers solved your question? Do you think you can accept one of them? See the [tour] how to do this, if you haven’t already done so. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).
– Maniero