zoukankan      html  css  js  c++  java
  • typescript 添加基础类型的扩展方法

    以时间转换为案例:

    //声明接口,也是在声明date这个基础类型要定义一个format的扩展方法,不写接口声明会报错
    interface Date {
        Format(fmt:string):string;
    }
    
    //要写到class类的外面,不然会划红线报错
    Date.prototype.Format = function (fmt) { //author: meizz   
        var o = {
            "M+": this.getMonth() + 1, //月份   
            "d+": this.getDate(), //
            "H+": this.getHours(), //小时   
            "m+": this.getMinutes(), //
            "s+": this.getSeconds(), //
            "q+": Math.floor((this.getMonth() + 3) / 3), //季度   
            "S": this.getMilliseconds() //毫秒   
        };
        if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    }

    调用:

    value.CreateTime = new Date(value.CreateTime).Format('yyyy-MM-dd');

  • 相关阅读:
    正则表达式
    iOS获取设备型号、设备类型等信息
    Dubbo-Zookeeper安装
    CentOS-常用安装
    多线程-线程通信
    JVM-高效并发
    静态代理与JDK动态代理
    JVM-类加载机制
    RPC原理及实现
    JVM-自动内存管理机制
  • 原文地址:https://www.cnblogs.com/llcdbk/p/10476562.html
Copyright © 2011-2022 走看看