美文网首页
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;
}

相关文章

  • TS 封装BeanUtils 对象拷贝工具类

    前言 项目中经常遇到对象浅层拷贝赋值的操作,如将A的对象值赋值给B,但是不能又不能合并,因为传参的时候不需要多余的...

  • ts通过class类封装axios

    ts通过class类封装axios 一、axios 安装 二、HttpRequest类封装 三、接口方法封装 四、...

  • typescript 入门教程二

    ts中面向对象成员修饰符:public , private , protexted(ts官方网站:ts) 在ts中...

  • 移动端自动播放视频之TS视频

    ts简介 TS(Transport Stream,传输流)是一种封装的格式,它的全称为MPEG2-TS。是一种视频...

  • ts (抄写的)

    继承 ts 数组对象vue

  • interface

    接口用于定义对象的 '轮廓' 但是如果我们直接传入一个对象字面量进去,ts就会报错, 因为ts对对象字面量检测更加...

  • Angular prod打包编译异常

    编译异常 问题描述:small对象在ts中不存在, 解决方案:如果small作为对象使用,在ts中添加small作...

  • vite-plugin-svg-icons插件封装SvgIcon

    通过 vite-plugin-svg-icons 插件封装SvgIcon组件 配置 vite.config.ts ...

  • 面试 | java基础

    面向对象 1、面向对象特征? 封装封装是面向对象的特征之一,是对象和类概念的主要特性。封装,也就是把客观事物封装成...

  • TS:对象池

    对象池项目源码: 使用:

网友评论

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

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