-23

I'm trying to get today's date

DateTime todayDateTime = new DateTime();

and I'm getting this:

{1/1/0001 12:00:00 AM}.

Why is this happening?

3
  • 3
    Show your code as well. It is the value of DateTime.MinValue field. May 28, 2014 at 13:49
  • DateTime todayDateTime = new DateTime(); May 28, 2014 at 13:51
  • 3
    Unlike Java's DateTime todayDateTime = new Date() which creates current datetime, C# returns "zero" datetime; use DateTime todayDateTime = DateTime.Now; in C# instead May 28, 2014 at 13:58

5 Answers 5

18

Use this

DateTime date = DateTime.Now;
1
  • 3
    DateTime date = DateTime.Today; is often the better choice.
    – ℍ ℍ
    May 28, 2014 at 13:55
5

Using new DateTime() creates a DateTime with a time of "0".

If you want todays date you need to use DateTime.Today if you want a DateTime object with a date of today and a time of 12:00:00 AM or DateTime.Now if you want a DateTime with the day and time of the moment you called DateTime.Now.

2

According to MSDN, the constructor for DateTime which takes in a long initializes by using the specified number of ticks since January 1st, 0001, so saying new DateTime(0) yields this time, not the current time.

Instead, use the static field DateTime.Now to get a DateTime representing the current system time.

0

In your question you are just initializing the Variable todayDateTime but you have never assigned (set it). This is why it is date ("null")/ beginning of our time calculations.

To actually get todays Date, you can use the following:

DateTime today = DateTime.Today;
0

first of all you need to assigned a value in the datetime. just use something like this :

DateTime today = DateTime.Today;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.