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
.
Related videos on Youtube
Author by
Cathy
Updated on September 14, 2022Comments
-
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 about 10 years
-
Jon Skeet about 10 yearsAlways include the error message in the question - or the exception, if there's an exception.
-
-
Jon Skeet about 10 yearsYou still need to fix
DateTime.now
toDateTime.Now
. -
vcsjones about 10 yearsYeah, I was in the middle of that. Done.
-
Bruce over 7 yearsThis 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.