gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 04:42:19 25 4
gpt4 key购买 nike

我从 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 (Якутское время (зима))

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

总结:我需要将输入日期格式转换为 Date obj 或通过 pg.js 日期字段(如 Date 对象)获取。我怎样才能从中做点什么?

最佳答案

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) 这对我的时区来说是正确的。

关于javascript - 如何为可编辑日期设置正确的日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26825433/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com