如何为可编辑日期设置正确的日期格式

时间:2014-11-09 05:53:26

标签: javascript angularjs date x-editable node-postgres

我从postgres获得一些日期文件,格式如下:

  

“2000-11-30T14:00:00.000Z”

我无法在页面上的可编辑日期字段中使用此功能。类似的东西:

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

此日期(如上所示)显示正常。但是,当我想编辑此字段时,日期将重置,我在控制台中收到此消息:

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)

如果我只是通过编辑字段来更新模型(输入新日期),将来可以编辑好,因为日期存储如(Date obj?):

  

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

如何将输入日期转换为角度格式可理解? 我还尝试用'new Date(输入日期 - 这里)替换输入日期格式,但它不起作用。可能是输入日期格式无法从字符串中解析?

总结:我需要将输入日期格式转换为Date obj或通过像Date对象这样的pg.js日期字段获取。我该怎么做呢?

2 个答案:

答案 0 :(得分:4)

Postgres以ISO 8601格式存储日期,Javascript Date可以开箱即用解析,例如:

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

结果为Thu Nov 30 2000 06:00:00 GMT-0800 (PST),这对我的时区是正确的。

答案 1 :(得分:2)

我找到了一个答案...我试图将输入日期转换为服务器端的Date obj,而不是将json发送给客户端。在客户端我总是把我的日期作为字符串,但不是Date对象。当我在客户端上做新的Date(inputDate)时,一切都还可以。