Vue自定义指令,时间转换

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>时间转换指令</title>
</head>
<body>

    <div id="app">
        <div v-time="timeNow"></div>
        <div v-time="timeBefore"></div>
    </div>

<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<script src="./js/time.js"></script>
<script>
    Vue.directive('time',{
        bind: function (el, binding) {
            el.innerHTML = Time.getFormatTime(binding.value);
            el.__timeout__ = setInterval(function () {
                el.innerHTML = Time.getFormatTime(binding.value);
            }, 60000);
        },
        unbind:function (el) {
            clearInterval(el.__timeout__);
            delete el.__timeout__;
        }
    });

    var app = new Vue({
        el: "#app",
        data: {
            timeNow: (new Date()).getTime(),
            timeBefore: 1488930695721
        }
    });
</script>
</body>
</html>

 time.js 时间转换:

var Time = {

    // 获得当前时间戳
    getUnix: function () {
        var date = new Date();
        return date.getTime();
    },

    // 补齐时间格式
    handleTime: function (val) {
        return val < 10 ? "0" + val : val;
    },

    // 获得今天0点0分0秒的时间戳
    getTodayUnix: function () {
        var date = new Date();
        date.setHours(0);
        date.setMinutes(0);
        date.setSeconds(0);
        date.setMilliseconds(0);
        return date.getTime();
    },

    // 获得今年1月1日0点0分0秒的时间戳
    getYearUnix: function () {
        var date = new Date();
        date.setMonth(0);
        date.setDate(1);
        date.setHours(0);
        date.setMinutes(0);
        date.setSeconds(0);
        date.setMilliseconds(0);
        return date.getTime();
    },

    // 获得标准年月日
    getLastDate: function (time) {
        var date = new Date(time);
        var month = this.handleTime(date.getMonth() + 1);
        var day = this.handleTime(date.getDate());
        return date.getFullYear() + "-" + month + "-" + day;
    },

    // 转换时间
    getFormatTime: function (timestamp) {
        // 当前的时间戳
        var now = this.getUnix();

        // 今天0点的时间戳
        var today = this.getTodayUnix();

        /**
         1分钟以前显示 刚刚
         1分钟到1小时之间 显示 xx 分钟之前
         1小时—— 1天之间  显示  xx 小时之前
         1天  - 31天之间  显示  xx 天之前
         大于 1 个月 显示 xx 年 xx 月 xx 日
         */

        var timer = (now - timestamp) / 1000;
        var tip = '';

        // 1分钟以前,显示“刚刚”。
        // 1分钟~1小时之间,显示“xx分钟前”。
        // 1小时~1天之间,显示“xx小时前”。
        // 1天~1个月(31天)之间,显示“xx天前”。
        // 大于1个月,显示“xx年xx月xx日”。

        if (timer <= 0) {
            tip = '刚刚';
        } else if (Math.floor(timer / 60) <= 0) {
            tip = '刚刚'
        } else if (timer < 3600) {
            tip = Math.floor(timer / 60) + '分钟前';
        } else if (timer >= 3600 && (timestamp - today >= 0)) {
            tip = Math.floor(timer / 3600) + '小时前'
        } else if (timer / 86400 <= 31) {
            tip = Math.ceil(timer / 86400) + '天前'
        } else {
            tip = this.getLastDate(timestamp);
        }
        return tip;
    }

};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值