C# DateTime (greetings)

Toz

Elite Member
Jr. VIP
Joined
Oct 24, 2011
Messages
3,219
Reaction score
3,625
Does anyone know you could perhaps create a while() that would greet someone differently, based on the time of the day (good {morning|afternoon|evening|night})?

I cannot seem to figure it out. :(
 
You should be using if Else statement instead of while.

Code:
if  (DateTime.Now <= 12){
    Console.WriteLine("Good Morning")
}else if  (DateTime.Now <= 16){
    Console.WriteLine("Good Afternoon")
}else if  (DateTime.Now <= 20){
    Console.WriteLine("Good Evening")
}else {
    Console.WriteLine("Good Night")
}

P.S. The code is for demonstration purpose only and won't work if you simply copy/paste it.

https://www.tutorialspoint.com/csharp/if_else_statement_in_csharp.htm
 
  • Like
Reactions: Toz
Thanks! I'd also just figured it out, while playing with a value I created with DateTime. I used the intellisense to literally scroll through everything!

This is what I did:

Code:
DateTime timeOfDayGreeting = DateTime.Now;
         
            if (timeOfDayGreeting.Hour >= 5 && timeOfDayGreeting.Hour < 12)
            {
                Console.WriteLine("Good morning!");
            }
            else if (timeOfDayGreeting.Hour >= 12 && timeOfDayGreeting.Hour < 16)
            {
                Console.WriteLine("Good afternoon!");
            }
            else
            {
                Console.WriteLine("Good night!");
            }

            Console.ReadKey();

Thanks for your input, though! OMG. I'm loving C#!


EDIT: Your code looks cleaner. I like your style!

Using your setup, I've come up with:
Code:
DateTime currentTime = DateTime.Now;

            if (currentTime.Hour < 12 && currentTime.Hour >= 5)
            {
                Console.WriteLine("Good Morning");
            }
            else if (currentTime.Hour >= 12)
            {
                Console.WriteLine("Good Afternoon");
            }
            else if (currentTime.Hour >= 16 )
            {
                Console.WriteLine("Good Evening");
            }
            else
            {
                Console.WriteLine("Good Night");
            }

            Console.ReadKey();
 
Last edited:
Thanks! I'd also just figured it out, while playing with a value I created with DateTime. I used the intellisense to literally scroll through everything!

This is what I did:

Code:
DateTime timeOfDayGreeting = DateTime.Now;
        
            if (timeOfDayGreeting.Hour >= 5 && timeOfDayGreeting.Hour < 12)
            {
                Console.WriteLine("Good morning!");
            }
            else if (timeOfDayGreeting.Hour >= 12 && timeOfDayGreeting.Hour < 16)
            {
                Console.WriteLine("Good afternoon!");
            }
            else
            {
                Console.WriteLine("Good night!");
            }

            Console.ReadKey();

Thanks for your input, though! OMG. I'm loving C#!


EDIT: Your code looks cleaner. I like your style!

Using your setup, I've come up with:
Code:
DateTime currentTime = DateTime.Now;

            if (currentTime.Hour < 12 && currentTime.Hour >= 5)
            {
                Console.WriteLine("Good Morning");
            }
            else if (currentTime.Hour >= 12)
            {
                Console.WriteLine("Good Afternoon");
            }
            else if (currentTime.Hour >= 16 )
            {
                Console.WriteLine("Good Evening");
            }
            else
            {
                Console.WriteLine("Good Night");
            }

            Console.ReadKey();
that's great bro
 
Better switch to Kotlin. It's fun
Not only we use "fun" keyword for function, but we also got fun interfaces

fun interface TimeOfDay: (Int) -> Boolean fun sayHi() { val morning = TimeOfDay { it in 8..11 } val afternoon = TimeOfDay { it in 12..15 } val evening = TimeOfDay { it in 16..24 } val we = object {} infix fun LocalDateTime.`is`(check: (Int) -> Boolean) = check(hour) infix fun Any.say(msg: String) = msg.also { println(it) } infix fun Any.drink(what: String) = println("$what mmm yumii") infix fun String.and(what: String) = apply { } LocalDateTime.now().let { when { it `is` morning -> we say "Good Morning" it `is` afternoon -> we say "Good Afternoon" it `is` evening -> we say "Good Evening" else -> we drink ("Coffee" and (we say "It's coding time")) } } }
 
Better switch to Kotlin. It's fun
Not only we use "fun" keyword for function, but we also got fun interfaces

fun interface TimeOfDay: (Int) -> Boolean fun sayHi() { val morning = TimeOfDay { it in 8..11 } val afternoon = TimeOfDay { it in 12..15 } val evening = TimeOfDay { it in 16..24 } val we = object {} infix fun LocalDateTime.`is`(check: (Int) -> Boolean) = check(hour) infix fun Any.say(msg: String) = msg.also { println(it) } infix fun Any.drink(what: String) = println("$what mmm yumii") infix fun String.and(what: String) = apply { } LocalDateTime.now().let { when { it `is` morning -> we say "Good Morning" it `is` afternoon -> we say "Good Afternoon" it `is` evening -> we say "Good Evening" else -> we drink ("Coffee" and (we say "It's coding time")) } } }
That looks so confusing...
Why do people create programming languages that just seem so.... overly complicated?
I think it makes them feel smarter.
 
That looks so confusing...
Why do people create programming languages that just seem so.... overly complicated?
I think it makes them feel smarter.
Boy oh boy, if that feels tough, try reading some rustlang code. ;) :D

On a different note, stick to what you know. Works most of the times.
 
Boy oh boy, if that feels tough, try reading some rustlang code. ;) :D
From reading through the Rust documentation, most of what I've seen of it makes sense (data types, functions, control flow, etc), though admittedly, I've not yet gotten my hands dirty with it.
This Kotlin language, however. It just seems complicated for the sake of being complicated.
 
From reading through the Rust documentation, most of what I've seen of it makes sense (data types, functions, control flow, etc), though admittedly, I've not yet gotten my hands dirty with it.
This Kotlin language, however. It just seems complicated for the sake of being complicated.
Hmmm it does make sense to use the “borrowing” concept (which is what scares me the most), but I find it really weird in practical use.
 
Hmmm it does make sense to use the “borrowing” concept (which is what scares me the most), but I find it really weird in practical use.
Rust is possibly on my todo list for 2023. It's either going to be Rust or Golang. I'm leaning more towards Rust, but haven't decided.
 
Rust is possibly on my todo list for 2023. It's either going to be Rust or Golang. I'm leaning more towards Rust, but haven't decided.
Golang is pretty easy to read atleast. I would suggest going for both, preferably rustlang first, as it will take time to get used to. Golang, I can promise you will learn quickly. :)
 
  • Like
Reactions: Toz
That looks so confusing...
that kotlin shit isnt overly complicated its just ugly as fuck.

That's not how we usually code. The idea was to create a line of code that says "When it is morning - we say good morning" with the least amount of brackets, like it ain't even written in a coding language, you guys missed the point :(
If I were to put here a usual example of how it should be done in kotlin that would be too boring!
 
That's not how we usually code. The idea was to create a line of code that says "When it is morning - we say good morning" with the least amount of brackets, like it ain't even written in a coding language, you guys missed the point :(
If I were to put here a usual example of how it should be done in kotlin that would be too boring!
Well that's good to know. Let's see the actual code for that example. Now I'm curious.
 
Let's see the actual code
val currentTime = LocalDateTime.now() val greetingMessage = when (currentTime.hour) { in 8..11 -> "Good Morning" in 12..15 -> "Good Afternoon" in 16..24 -> "Good Evening" else -> "Good Night" } println(greetingMessage)

In Kotlin you can use the "when" statement (aka "switch case") to compare numbers against ranges, like there currentTime.hour is compared against whether it is in 8..11 range.
You won't find this in Python, C/C++ or Java. Not sure about C#, its devs are constantly adding new features to the language to look more modern.
 
#include <stdio.h>
#include <time.h>

int main() {
time_t currentTime;
struct tm *localTime;

time(&currentTime);
localTime = localtime(&currentTime);

int hour = localTime->tm_hour;

if (hour >= 5 && hour < 12) {
printf("Good morning!\n");
} else if (hour >= 12 && hour < 17) {
printf("Good afternoon!\n");
} else if (hour >= 17 && hour < 20) {
printf("Good evening!\n");
} else {
printf("Good night!\n");
}

return 0;
}
You can create a C program that greets someone differently based on the time of day
 
Using your setup, I've come up with:
Code:
DateTime currentTime = DateTime.Now;

            if (currentTime.Hour < 12 && currentTime.Hour >= 5)
            {
                Console.WriteLine("Good Morning");
            }
            else if (currentTime.Hour >= 12)
            {
                Console.WriteLine("Good Afternoon");
            }
            else if (currentTime.Hour >= 16 )
            {
                Console.WriteLine("Good Evening");
            }
            else
            {
                Console.WriteLine("Good Night");
            }

            Console.ReadKey();
You probably already noticed but, this code does not work as intended.

Only you'll have with this code is "Good Morning" from 5:00:00 To 11:59:59, and "Good Afternoon" for the rest.

You'll need to set boundaries explicitly for all.

e.g. for "Good Afternoon":
C#:
... currentTime.Hour >= 12 && currentTime.Hour < 17 ...
 
  • Like
Reactions: Toz
Back
Top