我的网页上有以下HTML
<input type="date" class="form-control" datepicker-popup ng-model="mdl.active_date"
is-open="opened" min-date="minDate" datepicker-options="dateOptions"
date-disabled="disabled(date, mode)" close-text="Close" />
mdl.active_date
设置为&#34; /日期(1437626784877)/&#34;在浏览器控制台中查看时。
我收到错误Datepicker指令:ng-model value must be a Date object, a number of milliseconds since 01.01.1970 or a string representing an RFC2822 or ISO 8601 date.
我在这里做错了什么?
答案 0 :(得分:1)
它不是angularjs的问题。你可以使用new Date(YourVariable)
。
var datesting='/Date(1437626784877)/';
$scope.today =new Date(parseInt(datesting.replace('/Date(', '')));
将日期更新为字符串输入。
请参阅JSFiddle
答案 1 :(得分:0)
JSON不支持将日期作为自己认可的数据类型。在序列化时(根据序列化程序),使用多种格式之一将日期序列化为字符串,并且有时在反序列化时将序列化反序列化为日期对象(浏览器默认情况下从不这样做,它不是JSON规范的一部分)。
格式类似于.NET如何在JSON中将日期序列化为字符串。在使用反序列化的JSON之前的接收端,您需要将日期字符串转换为日期对象。 Angular或JSON.parse
都没有为你做这件事。
您可以使用$httpProvider.defaults.transformResponse
注入自定义解析。
https://docs.angularjs.org/api/ng/service/ $ HTTP
此处提供了包含日期支持的JSON.parse
实施:
http://weblog.west-wind.com/posts/2014/Jan/06/JavaScript-JSON-Date-Parsing-and-real-Dates
if (window.JSON && !window.JSON.dateParser) {
var reISO = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*))(?:Z|(\+|-)([\d|:]*))?$/;
var reMsAjax = /^\/Date\((d|-|.*)\)[\/|\\]$/;
JSON.dateParser = function (key, value) {
if (typeof value === 'string') {
var a = reISO.exec(value);
if (a)
return new Date(value);
a = reMsAjax.exec(value);
if (a) {
var b = a[1].split(/[-+,.]/);
return new Date(b[0] ? +b[0] : 0 - +b[1]);
}
}
return value;
};
}
var date = JSON.parse(json,JSON.dateParser);
console.log(date);