0

I am sending several properties as json, the datetime property is transferred some kind of broken, all others are fine.

How I receive a datetim property in javascript:

Created: "/Date(1441198490467+0200)/"

This would be the desired date: 02.09.2015 14:54:50

enter image description here

How can I fix this?

C#:

Webservice:

    [System.ServiceModel.OperationContract]
    [System.ServiceModel.Web.WebGet(UriTemplate = "PeekCustomer", ResponseFormat = WebMessageFormat.Json)]
    System.Collections.Generic.List<BusinessObjects.Customer> PeekCustomers();

The date property:

    public DateTime Created {
        get;
        set;
    }

Accessing the data:

while (reader.Read()) {
      result.Add(new BusinessObjects.Customer {
          ID = reader.GetGuid(0),
          Name = reader.GetString(1),
          Created = reader.GetDateTime(2)
      });
}

Javascript(Angular):

app.config(function(RestangularProvider){
   RestangularProvider.setBaseUrl('http://localhost:31896/BusinessService.svc/');
   RestangularProvider.setDefaultRequestParams('jsonp', { callback:    'JSON_CALLBACK' });
   RestangularProvider.setFullResponse(true);
});

The service:

    Restangular.all('').customGET('PeekCustomer').then(function (result){
        data.customers = result;
    })
AJ_83
  • 289
  • 1
  • 8
  • 22

2 Answers2

1

Well, your date is transferred as a date in JSON. You can transform it back on the client to the needed format (even correct for the timezone the user is in, for instance). It seems your clientscript isn't evaluating the date format back to script (and as not supplied what data.customers does it's only guessing).

As a workaround, you can change the datatype of the Created property to a string and format it with the right format string. But in general, check your client script first.

Niels V
  • 1,005
  • 8
  • 11
0

Finally after hourse of searching I found somebody who encountered the same problem:

How do I format a Microsoft JSON date?

Here is the code:

//service:
Restangular.all('').customGET('PeekCustomer').then(function (result){
    data.customers = angular.copy(result);
    data.customers = data.customers.data;

    for(var i = 0; i <= data.customers.length - 1; i++) {
        data.customers[i].Created = new Date(parseInt(data.customers[i].Created.substr(6)));
    }
})

//markup:
{{customer.Created | date:"yyyy-MM-dd HH:mm"}}

JSON date was the keyword.

Community
  • 1
  • 1
AJ_83
  • 289
  • 1
  • 8
  • 22