function zero(val){
    if(val < 10){
        return '0' + val;
    }
    return '' + val;
}

function parseDate(val){
    if(typeof val === 'string'){//去除中文
        val = val.replace('年', '-');
        val = val.replace('月', '-');
        val = val.replace('日', ' ');
        val = val.replace('时', ':');
        val = val.replace('分', ':');
        val = val.replace('秒', '');
    }

    var date;
    try{
        date = new Date(val);
    }catch(e){
        console.error(e);
        return undefined;
    }

    return {
        year: date.getFullYear(),
        month: zero(date.getMonth() + 1),
        day: zero(date.getDate()),
        hours: zero(date.getHours()),
        minutes: zero(date.getMinutes()),
        seconds: zero(date.getSeconds()),
    }
}

/**
 * 日期格式化过滤器
 * {{1567413182120 | date('yyyy/MM/dd hh:mm:ss')}}
 */
export default {
    name: 'date',
    handle: function(val, format = "yyyy-MM-dd"){
        var date = parseDate(val);
        if(!date){
            return val;
        }

        format = format.replace('yyyy', date.year);
        format = format.replace('YYYY', date.year);

        format = format.replace('MM', date.month);

        format = format.replace('dd', date.day);
        format = format.replace('DD', date.day);

        format = format.replace('hh', date.hours);
        format = format.replace('HH', date.hours);

        format = format.replace('mm', date.minutes);

        format = format.replace('ss', date.seconds);
        format = format.replace('SS', date.seconds);

        return format;
    }
}