time.js 853 Bytes
import _ from 'lodash';
import moment from 'moment';

export default {
    // unix timestamp format
    // use example:
    // v-time="1495787643"
    // v-time="{time: 1495787643}"
    // v-time="{time: 1495787643, format: 'moment支持的format格式'}"
    bind(el, binding) {
        let format;
        let unixStamp;
        const value = binding.value;
        const isNum = _.isFinite(value);
        const isObj = _.isPlainObject(value);
        const defaultFmt = 'YYYY-MM-DD HH:mm:ss';

        if (isNum) {
            unixStamp = value;
            format = defaultFmt;
        } else if (isObj) {
            unixStamp = binding.value.time;
            format = binding.value.format || defaultFmt;
        } else {
            unixStamp = 'Invalid value.';
        }

        el.innerHTML = moment.unix(unixStamp).format(format);
    }
};