DateTime devuelve una fecha incorrecta

I'm trying to get today's date

DateTime todayDateTime = new DateTime();

and I'm getting this:

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

¿Por qué está pasando esto?

preguntado el 28 de mayo de 14 a las 14:05

Show your code as well. It is the value of DateTime.MinValue campo. -

DateTime todayDateTime = new DateTime(); -

A diferencia de Java DateTime todayDateTime = new Date() which creates current datetime, C# returns "zero" datetime; use DateTime todayDateTime = DateTime.Now; in C# instead -

5 Respuestas

Use

DateTime date = DateTime.Now;

contestado el 28 de mayo de 14 a las 14:05

DateTime date = DateTime.Today; is often the better choice. - HH

Usar 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.

contestado el 28 de mayo de 14 a las 14:05

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

Instead, use the static field DateTime.Ahora conseguir un DateTime representing the current system time.

Respondido 07 Feb 17, 11:02

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;

Respondido el 25 de Septiembre de 18 a las 13:09

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

DateTime today = DateTime.Today;

Respondido 30 Abr '20, 22:04

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.