Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 132,685 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,259 people online right now. Registration is fast and FREE... Join Now!




using DateTime to get the current time

 
Reply to this topicStart new topic

using DateTime to get the current time, have I done this right?

OliveOyl3471
post 29 May, 2008 - 09:50 PM
Post #1


It's all about the code ♥

Group Icon
Joined: 11 Jul, 2007
Posts: 1,503



Thanked 14 times

Dream Kudos: 125
My Contributions


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.");
}

User is offlineProfile CardPM

Go to the top of the page

zakary
post 30 May, 2008 - 03:15 AM
Post #2


D.I.C Regular

Group Icon
Joined: 15 Feb, 2005
Posts: 401



Thanked 6 times

Dream Kudos: 175
My Contributions


the way you have it is good simple to read and understand,
User is offlineProfile CardPM

Go to the top of the page

djkitt
post 30 May, 2008 - 06:53 AM
Post #3


D.I.C Head

**
Joined: 22 May, 2008
Posts: 128



Thanked 13 times
My Contributions


You could simpify it a little.

CODE

            DateTime time = DateTime.Now;
            if (time.Hour < 12)
                Console.WriteLine("Good morning");
            else if (time.Hour < 18)
                Console.WriteLine("Good afternoon");
            else
                Console.WriteLine("Good Evening");

User is offlineProfile CardPM

Go to the top of the page

OliveOyl3471
post 31 May, 2008 - 10:00 PM
Post #4


It's all about the code ♥

Group Icon
Joined: 11 Jul, 2007
Posts: 1,503



Thanked 14 times

Dream Kudos: 125
My Contributions


Thank you. smile.gif
User is offlineProfile CardPM

Go to the top of the page

eclipsed4utoo
post 2 Jun, 2008 - 07:10 AM
Post #5


D.I.C Regular

Group Icon
Joined: 21 Mar, 2008
Posts: 314



Thanked 17 times

Dream Kudos: 25
My Contributions


QUOTE(djkitt @ 30 May, 2008 - 07:53 AM) *

You could simpify it a little.

CODE

            DateTime time = DateTime.Now;
            if (time.Hour < 12)
                Console.WriteLine("Good morning");
            else if (time.Hour < 18)
                Console.WriteLine("Good afternoon");
            else
                Console.WriteLine("Good Evening");



or simplify it a little more...

C#
			
if (DateTime.Now.Hour < 12)
Console.WriteLine("Good morning");
else if (DateTime.Now.Hour < 18)
Console.WriteLine("Good afternoon");
else
Console.WriteLine("Good Evening");
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 07:15AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month