Michael
Michael

Reputation: 13616

Any idea why in client I get only part of variable DateTime?

I use web api and angularjs in my project.

I have this class:

  public class DamageEvent
    {
       public int Id { get; set; }
       public int? ClientId { get; set; }
       public DateTime? CreateDate { get; set; }
    }

From client side I use http service to call this action method:

    public async Task<IHttpActionResult> RetriveDamageEvents()
    {
        try
        {       
            //Using Linq to Entity to get data from DataBase.
            DamageEvent damageEvent = _repository.Get().Where(some logic).SingleOrDefault();
            //The content of damageEvent after retriving from database is:
            //damageEvent = {id = 1, ClientId = 25, CreateDate = 5/1/2016 5:26:14 PM}
            return Ok(damageEvent);
        }
        catch (Exception)
        {
            return BadRequest();
        }
    }

But when I recieve it in client, I use debugger watch to check the value and it looks like that:

{
   id : 1, 
   ClientId : 25, 
   CreateDate : "5/1/2016"
}

As you can see in client side the CreateDate property in object damageEvent this part of variable is missing:

5:26:14 PM

Any idea why in client I get only part of variable DateTime?Why hours, minutes and seconds are missing?

Upvotes: 1

Views: 31

Answers (1)

ManOVision
ManOVision

Reputation: 1893

Sounds like a serialize issue. In your App_Start/WebApiConfig.cs file you can set your serializer settings.

config.Formatters.JsonFormatter.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;

I recommend all dates be stored as UTC and then your client can decide how to show that. This way every date in your system is consistent.

Upvotes: 1

Related Questions