angular將日期格式20180101過濾成其他格式
阿新 • • 發佈:2018-12-11
呼叫方式同date內建過濾
const angular = require('angular');
const MODULE_NAME = 'dateFilter';
const MODULE = angular.module(MODULE_NAME, []);
MODULE.filter("dateFilter", function($filter) {
return function(input, format) {
if(!isNaN(Number(input))&&String(input).length==8){
let date = '';
input = String(input);
date += input.slice(0,4);
date += '/';
date += input.slice(4,6);
date += '/';
date += input.slice(6,8);
var stamp = new Date(date).getTime();
return $filter('date')(stamp, format);
}else {
return '';
}
}
});
module.exports = MODULE_NAME;