I want my program to print a greeting based on the user's current time (whatever time the computer displays at the moment).
This code works, but is it done the right way? Is there a better way of doing this?
csharp
//Depending on the current time, print either "Good morning," "Good afternoon," or "Good evening"
DateTime time = new DateTime();
//get the current time
time = DateTime.Now;
//if it is past midnight but before noon, print "Good morning."
if (time.Hour >=0 && time.Hour < 12 )
{
Console.WriteLine("Good morning.");
}
//if it is past noon but before 6PM, print "Good afternoon."
else if (time.Hour >= 12 && time.Hour < 18)
{
Console.WriteLine("Good afternoon.");
}
//if it is past 6PM, print "Good evening."
else if (time.Hour >= 18)
{
Console.WriteLine("Good evening.");
}