首页 > 其他 > 详细

vue 封装时间戳 用过滤方法封装

时间:2019-11-21 18:16:54      阅读:196      评论:0      收藏:0      [点我收藏+]
Vue.filter("formatTime",function(value,type,split,isZero){
    var dataTime=value;
    if(!type){
        type="YMDHMS";
    }
    if(typeof(value)!=‘string‘){
        if(value.toString().length==10||value.toString().length==13){
            var split_ = split ? split : "-";
            var data = new Date();
            data.setTime(value);
            var year   =  data.getFullYear();
            var month  =  ((data.getMonth()+1 <10&&isZero) ? ‘0‘+(data.getMonth()+1) : data.getMonth()+1);
            var day    =  ((data.getDate()<10&&isZero)?(‘0‘+data.getDate()): data.getDate());
            var hour   =  ((data.getHours()<10&&isZero)?(‘0‘+data.getHours()): data.getHours());
            var minute =  ((data.getMinutes()<10&&isZero)?(‘0‘+data.getMinutes()): data.getMinutes());
            var second =  ((data.getSeconds()<10&&isZero)?(‘0‘+data.getSeconds()): data.getSeconds());
            if(type == "YMD"){
                dataTime =  year + split_+ month + split_ + day;
            }else if(type == "HMS"){
                dataTime = hour+":" + minute+":" + second;
            }else {
                dataTime = year + split_+month + split_ + day + split_ +hour+ ":"+minute+":" +second;
            }
        }
    }
    return dataTime;
});

value 值

type 时间类型 两种 年月日 跟 时分秒 例如 2020-01-01-24:58 ,24:58

split 下划线 横杠等  例如 2020-01-01 ,2020/01/01

iszero 是否为加 0   布尔值true跟false   例如 2020-01-01 ,2020-1-1

用法 :时间戳  I  formatTime(value,type,split,isZero)

 

vue 封装时间戳 用过滤方法封装

原文:https://www.cnblogs.com/chen527/p/11906921.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!