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?
Use this
DateTime date = DateTime.Now;
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
.
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.
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;
first of all you need to assigned a value in the datetime. just use something like this :
DateTime today = DateTime.Today;
DateTime.MinValue
field.DateTime todayDateTime = new Date()
which creates current datetime, C# returns "zero" datetime; useDateTime todayDateTime = DateTime.Now;
in C# instead