Printing current date and time C#

18,977

Solution 1

There are a few issues here. First, your class is called DateTime, which is causing the compiler to confuse it with System.DateTime. Also make sure the System namespace is available with a using directive, or fully-qualifying the type. Thirdly, C# is case sensitive. It should be Now, not now. Try giving things different names:

using System;
namespace MyNamespace
{
    class MyClass
    {
        public static void Main()
        {
            DateTime now = DateTime.Now;
            Console.WriteLine(now);
            Console.ReadLine();
        }
    }
}

Solution 2

If you getting this error:

'System.DateTime' does not contain a definition for 'now'

Try DateTime.Now with uppercase 'N' instead DateTime.now.

Solution 3

A typo: DateTime.now. Now should start with the capital letter N as follows: DateTime.Now.

Share:
18,977

Related videos on Youtube

Cathy
Author by

Cathy

Updated on September 14, 2022

Comments

  • Cathy
    Cathy over 1 year

    That's an easy one, but there is an error that appears. I have to create a console application for printing current date and time.

    Here is my code:

    namespace DateTime
    {
        class DateTime
        {
            public static void Main()
    
            {
                DateTime now = DateTime.now;
                Console.WriteLine(now);
                Console.ReadLine();
            }
    }
    }
    

    I'm using visual studio 2012.

    • Soner Gönül
      Soner Gönül about 10 years
      It is .Now not .now with capital N. C# is case sensitive.
    • Jon Skeet
      Jon Skeet about 10 years
      Always include the error message in the question - or the exception, if there's an exception.
  • Jon Skeet
    Jon Skeet about 10 years
    You still need to fix DateTime.now to DateTime.Now.
  • vcsjones
    vcsjones about 10 years
    Yeah, I was in the middle of that. Done.
  • Bruce
    Bruce over 7 years
    This does not provide an answer to the question. Once you have sufficient reputation you will be able to [comment ](stackoverflow.com/help/privileges/comment) on any post; instead, provide answers that don't require clarification from the asker.