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

(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
7 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
daCypher
1 year ago

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.

Erzesel
1 year ago

the DateTime object brings everything to direct comparisons (without any great magic)

The method DateTime.Compare() is exactly what you need:

using System;
class Prog{
    public static void Main(string[] args){
        string tageszeit="grummel";
        DateTime jetzt = DateTime.Now;
        Console.WriteLine(jetzt);
        Console.WriteLine(DateTime.Compare(jetzt, Convert.ToDateTime("12:00"))); //immer -1,0 oder 1


        if (DateTime.Compare(jetzt, Convert.ToDateTime("04:00")) >= 0) tageszeit = "Morgen"; 
        if (DateTime.Compare(jetzt, Convert.ToDateTime("12:00")) >= 0) tageszeit = "Tag";
        if (DateTime.Compare(jetzt, Convert.ToDateTime("18:00")) >= 0) tageszeit = "Abend";
        Console.WriteLine("Guten {0}",tageszeit);
        Console.ReadKey();
    }
}

I personally like to work with Timespan better:

using System;
class Prog{
    public static void Main(string[] args){
        string tageszeit="grummel";
        DateTime jetzt = DateTime.Now;
        DateTime mitternacht = Convert.ToDateTime("0:00");
        TimeSpan seitMitternacht = jetzt - mitternacht;
        Console.WriteLine("Zeitspanne seit Mitternacht: {0});",seitMitternacht) ;


        if (seitMitternacht >= TimeSpan.Parse("04:00"))  tageszeit="Morgen";
        if (seitMitternacht >= TimeSpan.Parse("12:00"))  tageszeit="Tag"; 
        if (seitMitternacht >= TimeSpan.Parse("18:00"))  tageszeit="Abend";
        Console.WriteLine("Guten {0}",tageszeit);


        Console.ReadKey();
    }
}

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)

Fabian229
1 year ago

Good evening,

you should do this with DateTime.Hour and with an if or switch.

For example:

// Retrieve time

    DateTime jetzt = DateTime.Now;
    int stunde = jetzt.Hour;
    string gruss;
    if (stunde >= 12 && stunde < 18)
    {
      gruss = "Guten Tag";
    }
    else if (stunde >= 18)
    {
      gruss = "Guten Abend";
    }
    else
    {
      gruss = "Guten Morgen";
    }
    Console.WriteLine(gruss);
  }
}
Erzesel
1 year ago
Reply to  Fabian229

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…

Fabian229
1 year ago
Reply to  Erzesel

Thanks for the tip

regex9
1 year ago

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.

if (time.Hour > 20)
{
  // ...
}