美文网首页
Date&&String对象 TS封装

Date&&String对象 TS封装

作者: 小米和豆豆 | 来源:发表于2021-02-06 16:42 被阅读0次
    date.ts (配合string.ts使用)
     interface Date{
        copy(): Date;
        clone(): Date;
        compare(date: Date): number;
        toString(format: string): string;
        customerString(format: string): string;
        addDays(day: number): void;
        addWeeks(week: number): void;
        addMonths(month: number): void;
        addYears(year: number): void;
        startTimeOfDate(): Date ;
        endTimeOfDate(): Date ;
        getDaysOfMonth(year:number,month:number):number;
    }
    /// <summary>
    /// 日期拷贝
    /// </summary>
    /// <return>日期对象</return>
    //本日开始时间
    Date.prototype.startTimeOfDate=function():Date{
        return new Date(new Date().setHours(8, 0, 0, 0));
    }
    //本日结束时间
    Date.prototype.endTimeOfDate=function():Date{
        return new Date(new Date(new Date().setHours(8, 0, 0, 0)).getTime() + 24 * 60 * 60 * 1000 - 1)
    }
    //某月天数
    Date.prototype.getDaysOfMonth=function(year,month):number{
        let d = new Date(year, month, 0);
        return d.getDate();
    }
    
    Date.prototype.copy = function (): Date {
        return new Date(this.getFullYear(), this.getMonth(), this.getDate());
    }
    Date.prototype.clone = function (): Date {
        let d = new Date(this.getTime());
        return d;
    }
    /*
     * 日期比较
     */
    Date.prototype.compare = function (date): number {
        return this.getTime() - date.getTime();
    }
    
    Date.prototype.toString = function (format = "yyyy-MM-dd"): string {
        // if (!format || typeof format != 'string')
        //     format = "yyyy-MM-dd";
    
        format = format.replace(/yyyy/, this.getFullYear() + '')
          .replace(/yy/, this.getFullYear().toString().substring(2))
          .replace(/y/, this.getFullYear().toString().substring(2).trimLeft_("0"))
          .replace(/MM/, (this.getMonth() + 1).toString().padLeft('0', 2)+'')
          .replace(/dd/, this.getDate().toString().padLeft('0', 2)+'')
            .replace(/M/, this.getMonth() + 1 + '')
          .replace(/d/, this.getDate().toString())
          .replace(/HH/, this.getHours().toString().padLeft('0', 2)+'')
          .replace(/hh/, (this.getHours() > 12 ? (this.getHours() - 12)+'' : this.getHours()).toString().padLeft('0', 2)+'')
          .replace(/mm/, this.getMinutes().toString().padLeft('0', 2)+'')
          .replace(/ss/, this.getSeconds().toString().padLeft('0', 2)+'')
          .replace(/H/, this.getHours()+'')
            .replace(/h/, (this.getHours() > 12 ? (this.getHours() - 12) : this.getHours()) + '')
            .replace(/m/, this.getMinutes() + '')
            .replace(/s/, this.getSeconds() + '');
        return format;
    }
    
    Date.prototype.addDays = function (d: number) {
        this.setDate(this.getDate() + d);
    };
    
    Date.prototype.addMonths = function (m) {
        var d = this.getDate();
        this.setMonth(this.getMonth() + m);
        if (this.getDate() < d)
            this.setDate(0);
    };
    
    Date.prototype.addYears = function (y) {
        var m = this.getMonth();
        this.setFullYear(this.getFullYear() + y);
        if (m < this.getMonth()) {
            this.setDate(0);
        }
    };
    
    string.ts
    interface String{
        toDate(format: string): Date;
        trimLeft_(ch: string): string;
        trimRight_(ch: string): string;
        padLeft(ch: string, len: number): String;
        padRight(ch: string, len: number): String;
    }
    /// <summary>
    /// 移除字符空白字符
    /// </summary>
    String.prototype.trim = function (){
        if (this == "") return '';
        return this.replace(/(^\s*)|(\s*$)/g, '');
    }
    
    /// <summary>
    /// 移除左侧字符
    /// </summary>
    /// <param name="str">字符串</param>
    /// <param name="ch">移除字符</param>
    /// <returns>移除后字符串</returns>
    String.prototype.trimLeft_ = function (ch:string) {
        if (this == "") return '';
        return this.replace(new RegExp('^' + ch + '*', 'g'), '');
    }
    
    /// <summary>
    /// 移除右侧侧字符
    /// </summary>
    /// <param name="str">字符串</param>
    /// <param name="char">移除字符</param>
    /// <returns>移除后字符串</returns>
    String.prototype.trimRight_= function (ch:string) {
        if (this == "") return '';
        return this.replace(new RegExp(ch + '*$', 'g'), '');
    }
    
    /// <summary>
    /// 左侧字符填充
    /// </summary>
    /// <param name="ch">填充字符</param>
    /// <param name="length">填充后字符串长度</param>
    /// <returns>填充后字符串</returns>
    String.prototype.padLeft = function(ch:string, length: number) {
        let str = this;
        for(let i=0;i< length - this.length; i++)
            str = ch + str;
      return str;
    }
    
    /// <summary>
    /// 右侧字符填充
    /// </summary>
    /// <param name="ch">填充字符</param>
    /// <param name="length">填充后字符串长度</param>
    /// <returns>填充后字符串</returns>
    String.prototype.padRight = function(ch:string, length: number) {
        let str = this;
        for(let i=0; i<length - str.length; i++)
            str = str + ch;
      return str;
    }
    
    

    相关文章

      网友评论

          本文标题:Date&&String对象 TS封装

          本文链接:https://www.haomeiwen.com/subject/gpmztltx.html