0

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.

1
  • Use Json.NET instead of the old serializer classes. They were created back when there was no preferred representation for dates Commented Oct 2, 2014 at 9:22

4 Answers 4

3

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>

Sign up to request clarification or add additional context in comments.

1 Comment

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
1

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

Comments

0

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

Comments

0

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 : )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.