根据Date filter documentation,date 参数应该是:
日期格式化为日期对象,毫秒(字符串或数字)
或各种 ISO 8601 日期时间字符串格式(例如
yyyy-MM-ddTHH:mm:ss.sssZ 及其较短的版本,如
yyyy-MM-ddTHH:mmZ、yyyy-MM-dd 或 yyyyMMddTHHmmssZ)。如果没有时区
在字符串输入中指定的时间被认为是在
当地时区。
因此,您需要将字符串转换为有效的 javascript 日期,或者创建自己的过滤器,该过滤器依赖于 angularjs 内置的日期过滤器
angular.module('app').filter('customDateFormat', function($filter) {
return function(value) {
if (value == null) {
return "";
}
return $filter('date')(new Date(value).toISOString(), 'medium');
};
});
然后在你的 html 中,
{{ thisDay | customDateFormat}}
更新:
此外,如果您想让您的过滤器更通用,并且将日期格式与您的日期一起传递,该怎么办:
angular.module('app').filter('customDate', function($filter) {
return function(value, format, defaultValue) {
if (value == null) {
if (defaultValue) {
return defaultValue;
} else {
return "";
}
}
return $filter('date')(new Date(value).toISOString(), format);
};
});
然后在你的 HTML 中:
{{thisDay | customDateFormat:'shortTime'}}