Reputation: 11
I have the following entity which has 2 datetimefields..
public class Contactos
{
public int IdContacto { get; set; }
public string PrimerNombre {
get;set;
}
//Code cut for brevity
public DateTime? FechaConsulta { get; set; }
public DateTime? FechaArchivado { get; set; }
}
then on my api I have this
[ResponseType(typeof(Contactos))]
public List<Contactos> GetContactosDepurar(DateTime fechaInicial)
{
var contactos = ContactosDao.GetContactosDepurados(fechaInicial);
return contactos.ToList();
}
and this
public static List<Contactos> GetContactosDepurados(DateTime fechaInicial)
{
var fecha = fechaInicial.ToString("yyyy/MM/dd");
string sql = @"SP_ConsultarContactosDepurar";
return Db.Read(sql, ContactosDepurar, new object[]
{
"@fechaInicial", fechaInicial
}).ToList();
}
static readonly Func<IDataReader, Contactos> ContactosDepurar = reader =>
new Contactos
{
IdContacto = reader["idContacto"].AsInt(),
PrimerNombre = reader["primerNombre"].ToString(),
FechaArchivado = reader["fechaArchivado"] is DBNull ? (DateTime?)null : DateTime.ParseExact(reader["fechaArchivado"].ToString(), "dd/mm/yyyy", CultureInfo.CurrentCulture),
FechaConsulta = reader["fechaConsulta"] is DBNull ? (DateTime?)null: DateTime.ParseExact(reader["fechaConsulta"].ToString(), "dd/mm/yyyy", CultureInfo.CurrentCulture)
};
The problem is that when checking the RETURN values on POSTMAN, the date field is being returned as a number:
"FechaConsulta": "/Date(1459832400000-0500)/",
Upvotes: 1
Views: 4188
Reputation: 21
[Route("api/[controller]")]
[ApiController]
public class DateTimeController : ControllerBase
{
[HttpGet]
public IActionResult GetDateTime()
{
DateTime CurrentdateTime = DateTime.Now;
return Ok(CurrentdateTime);
}
}
Upvotes: 0
Reputation: 1075
A quick synapses is to realize that a DateTime is just a number sequence and that is how it is handled internally. When you want to see the value of DateTime you will most likely want to format that into a textual representation of the date.
So instead of returning FechaConsulta and FechaArchivado, you should be returning
FechaConsulta.ToString("yyyy/MM/dd")
and FechaArchivado.ToString("yyyy/MM/dd")
For a relative quick reading on what DateTime is and how to work with it, please read the answer I posted on a question about how SQL Server stores a DateTime, how the Application works with it, and how to change display formats Sql saving date with incorrect format
Upvotes: 0
Reputation: 72
Just need to convert the time to your local time using the built in Time Functions. Take your pick from here:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms725473(v=vs.85).aspx
Upvotes: 1
Reputation: 1275
I would pass a Unix/Epoch time stamp for the date. I work with Stripe quite a bit and this is how they pass all their datetimes. I adapted my answer from this post.
var myDate = new DateTime(2017, 5, 5);
int unixTimestamp = (int)(myDate.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
Upvotes: 0