as a beginner i try my hand to write different small codes to console application . but am having a prolbem here due to the fact that am not so aspect as yet. can some one help me write it well.

i want my code to output the name of day of the week when i enter a date . for example when i enter 23.12.1993. the computer should output it is a wednesday.


using System;
public class Daydemo
{
public static void Main()
{
int dt = ();
DateTime Dato = DateTime.Now;
dt = DateTime.Dato;

Console.WriteLine("The time is:"+dt);
}
}

Recommended Answers

All 5 Replies

Try this

DateTime myDate = new DateTime(1993, 12, 23);
   Console.WriteLine(string.Format("The day in letters is :{0}",myDate.DayOfWeek.ToString()));
            Console.Read();

wow ja, that was helpfull, i could see my mistakes and what i have to be awware of.

But what if i now want to polish this code to be more dymamic , for example to ask the user to enter any date and the output give the answer to be to the user " the date you entered was on tuesday or wednesday and so forth.

Console.WriteLine(" enter your date")

then i expected the computer to do the calculations.
thanks

try
{
     Console.WriteLine("Insert a date:"); 
     string strDate = Console.ReadLine();
    
     DateTime myDate = Convert.ToDateTime(strDate );

     Console.WriteLine(string.Format("The day in letters is :{0}",myDate.DayOfWeek.ToString()));
    
     Console.Read();

}
catch(Exception)
{
       Console.WriteLine("Sorry that's not a valid date.");
}

thank you hollystyles

it worked so nice,
except that i have to set catch(exception ) as comment. then it worked.

thanks , i tried it with my birthday, so now i know which day i was born . wow, wow,

anyway can you explain to me what catch exception can do in that code.

except that i have to set catch(exception ) as comment.

What? C# is case sensitive the Exception class is with a capital E.

Console.Readline returns a string of character data input by the user of the program, the user may deliberately or mistakenly enter a string that is not valid as a date 30/Feb/1999 for example. In that situation the Convert.ToDateTime will crash the programme. The try catch syntax enables us to handle the situation gracefully and politely tell the user the input is not valid rather than just allow the program to crash and terminate. Any error that happens in the code inside a try block between the try opening and closing braces {} causes the code in the catch block to be executed.

Run the code in the debugger (assuming your using Visual Studio just press F5) set a break point on the Convert.ToDateTime line of code (just click in the left margin with the mouse for that line so a red spot appears) Enter some random input in the console window that isn't a date. and press enter. The program will pause at the break point, press the F10 key (F10 is for stepping through your code one line of execution at a time.) See how it leaps into the catch block. Now repeat these steps but enter a valid date and step through the code using F10 again. See how with a valid date the catch block is not executed but skipped over.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.