Mit der Uhrzeit programmieren in C#?
Guten Abend, ich möchte ein kleines Projekt in C# schreiben, das Programm soll mich auf verschiedenen Uhrzeiten verschieden grüßen zb soll mir das programm ab 12 Uhr sagen “Guten Tag” und ab 18 Uhr “Guten Aben”
Ich habe versucht eine Int mit dem Wert der System Uhrzeit zu speichern hat aber alles nicht nach plan geklappt, dass alles habe ich in einer if frage gemacht wahr aber trozdem falsch.
bitte um Hilfe. Danke im voraus 🙂
ich Hoffe man erkennst das bild, fals nicht dann gerne melden
time is in the case a complete date with time. You can’t just compare it to a number. I’m just writing on the phone, so I can’t test, but if you write a point behind time, Visual Studio would have to show you all the features and methods you can use on a DateTime object. The current hour is called Hour or HourOfDay. You can then check if it is larger or smaller than 20.
the DateTime object brings everything to direct comparisons (without any great magic)
The method DateTime.Compare() is exactly what you need:
I personally like to work with Timespan better:
In this case, the daily times refer to the time that has elapsed since midnight, a period of time results from simple subtracion of two DateTimes.
the advantage of this approach, you always have access to a real number with which many other operations can be carried out (via TimSpan .Total…) (TotalHours ..TotalNanoseconds)
Good evening,
you should do this with DateTime.Hour and with an if or switch.
For example:
// Retrieve time
Many thanks to you, now I can finally write my program 🙂
You really need the other constructs nich…
Simply blunt a chain of options off, as repeated setting the same variable in this simple case does not represent a time-relevant problem. the last matching value remains…
Thanks for the tip
The DateTime-struct forms the information of a complete timestamp, so is more than just a number.
To get the current hour, you can Hour– Call Property.