Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have

public IEnumerable<object> GetStudentDate(string firstname)
{
    var query = from usr in context.Student
                where (usr.hasDateTime == true && (usr.LastName.Contains(lastname))
                select new
                {
                    CDate = usr.Date;
                    return query;
                }

And i want to use it with AngularJs

UserDate {{user.CDate}}

But i only get something like this: UserDate /Date(1420875802707)/

How can i parse this DateTime to this Date or something like they said in reference:(Angular Ref) {{1288323623006 | date:'medium'}}

When i do usr.Date.ToString() formating with angular doesnt work.

share|improve this question
    
Use Json.NET instead of the old serializer classes. They were created back when there was no preferred representation for dates –  Panagiotis Kanavos Oct 2 '14 at 9:22

4 Answers 4

You can create your own filter to transform Microsoft JSON date to js date ie:

var app = angular.module('app',[]);
app.filter('ctime', function(){
  
  return function(jsonDate){
    
    var date = new Date(parseInt(jsonDate.substr(6)));
    return date;
  };
  
});
app.controller('fCtrl', function($scope){
  
  $scope.date = '/Date(1420875802707)/';
  
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

 <div ng-app="app">
    <div ng-controller="fCtrl">
     Raw Date : {{date}}
      <hr/>
     Custom Filter : {{date|ctime | date : 'medium'}}
      <hr/>
     Custom Filter + Angularjs Filter  :{{date|ctime | date : 'short'}}
    </div>
    </div>

share|improve this answer
    
No need for that, just use Json.NET to send properly serialized Json. The library is included in all ASP.NET MVC projects out of the box, and it's become the recommended way to work with Json –  Panagiotis Kanavos Oct 2 '14 at 9:21

I suspect you use either the JavaScriptSerializer or DataContractJsonSerializer serializers to convert your objects to Json, which serialize dates in this format. These classes have limited JSon functionality and were created mainly to support early AJAX applications. They shouldn't be used if you want proper JSon support.

Instead of these classes, use Json.NET, which does support the ISO8601 format and supports far more JSon features. Project templates in Visual Studio 2012+ include Json.NET by default. There's no need to try and convert the date at the client side using any external libraries.

Json itself doesn't specify a date format so early attempts at serializing data from the server to the client just used what seemed appropriate. There's still no standard representation but the ECMA standard defines the ISO format as the "preferred representation".

share|improve this answer
    
+1 great explanation. –  Christos Oct 2 '14 at 9:29

shall use MomentJS which can handle .net date formats (beware its one way only(.NET to Javascript) AFAIK)

share|improve this answer

i did that: in controller i used for loop to jump over all objects and used parseInt(obj.substr(6)) and it now works pretty nice : )

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.