How to set right date format for editable-date

Alendorff picture Alendorff · Nov 9, 2014 · Viewed 8.8k times · Source

I getting some date fileds from postgres with format like:

"2000-11-30T14:00:00.000Z"

I can't use this in my editable-date field on page. Something like:

 <a href="#" editable-date="employee.brthday"
     onbeforesave="updateField($data, 'brthday', employee)">
         {{employee.brthday || 'empty' | date:"dd/MM/yyyy" }}
 </a>

This date (like above) displayed fine. But when I want edit this field, date will reset and I got this message in console:

Error: [ngModel:datefmt] Expected `2000-12-05T14:00:00.000Z` to be a date http://errors.angularjs.org/1.3.0/ngModel/datefmt?p0=2000-12-05T14%3A00%3A00.000Z
    at http://localhost:8000/bower_components/angular/angular.js:80:12
    at Array.<anonymous> (http://localhost:8000/bower_components/angular/angular.js:19453:17)
    at Object.ngModelWatch (http://localhost:8000/bower_components/angular/angular.js:20555:36)
    at Scope.$digest (http://localhost:8000/bower_components/angular/angular.js:13957:40)
    at Scope.$apply (http://localhost:8000/bower_components/angular/angular.js:14227:24)
    at HTMLAnchorElement.<anonymous> (http://localhost:8000/bower_components/angular-xeditable/dist/js/xeditable.js:831:21)
    at HTMLAnchorElement.jQuery.event.dispatch (http://localhost:8000/bower_components/jquery/dist/jquery.js:4409:9)
    at HTMLAnchorElement.elemData.handle (http://localhost:8000/bower_components/jquery/dist/jquery.js:4095:28)

If I just update model by editing the field (input new date), It could be edit fine in future, because date stored like (Date obj?):

Wed Dec 06 2000 00:00:00 GMT+1000 (Якутское время (зима))

How can I convert my input date, to understandable for angular format?
I also tried to replace input date format with 'new Date(input-date-here), but it doesn't work. May be input date format can't be parsed just from string?

Summarizing: I need convert input date format to Date obj OR get via pg.js date fields like Date objects. How can I do something from that?

Answer

sherb picture sherb · Nov 9, 2014

Postgres is storing the dates in ISO 8601 format, which Javascript Date can parse out of the box, for example:

var x = new Date("2000-11-30T14:00:00.000Z");
console.log(x);

results in Thu Nov 30 2000 06:00:00 GMT-0800 (PST) which is correct for my time zone.