美文网首页
自定义时间类

自定义时间类

作者: Agneszbaby | 来源:发表于2019-04-19 15:03 被阅读0次

js获取时间
获取年、月、日、周
格式化日期
获取日期范围
获取历史前N天的时间
获取未来N天的时间

/**
 * 自定义时间类
 */
class TimeClass {
    constructor(date) {
        if (!date || typeof (date) === "string") {
            date = new Date();
        }
        this.date = date;

        this.year = this.getCurrYear();
        this.month = this.getCurrMonth();
        this.day = this.getCurrDay();
        this.week = this.getCurrWeek();
        this.hours = this.getCurrHours();
        this.minutes = this.getCurrMinutes();
        this.seconds = this.getCurrSeconds();
    }

    /**
     * 格式化时间
     * @param date
     * @returns {string}
     */
    timeFormat(date) {
        if (date) {
            this.date = date;
        }
        return `${this.getCurrYear()}-${this.getCurrMonth()}-${this.getCurrDay()}`;
    }

    /**
     * 获取年份
     * @returns {number}
     */
    getCurrYear() {
        return this.date.getFullYear();
    }

    /**
     * 获取月份
     * @returns {*}
     */
    getCurrMonth() {
        let ominth = this.date.getMonth() + 1;
        return ominth > 9 ? ominth : '0' + ominth;
    }

    /**
     * 获取本日
     * @returns {any}
     */
    getCurrDay() {
        let oDay = this.date.getDate();
        return oDay > 9 ? oDay : '0' + oDay;
    }

    /**
     * 获取周几
     * @returns {number}
     */
    getCurrWeek() {
        return this.date.getDay();
    }

    /**
     * 获取时
     * @returns {number}
     */
    getCurrHours() {
        return this.date.getHours();
    }

    /**
     * 获取分
     * @returns {number}
     */
    getCurrMinutes() {
        return this.date.getMinutes();
    }

    /**
     * 获取秒
     * @returns {number}
     */
    getCurrSeconds() {
        return this.date.getSeconds();
    }

    /**
     * 获取本周开始日期
     * @returns {string}
     */
    getCurrWeekStart() {
        var oWeek = this.getCurrWeek();
        oWeek = oWeek > 0 ? oWeek : 7;
        return this.computeTime(-oWeek + 1);
    }

    /**
     * 获取本周结束日期
     * @returns {string}
     */
    getCurrWeekEnd() {
        var oWeek = this.getCurrWeek();
        oWeek = oWeek > 0 ? oWeek : 7;
        return this.computeTime(7 - oWeek);
    }

    /**
     * 获取本周日期范围
     * @returns {string[]}
     */
    getCurrWeekScope() {
        let startTime = this.getCurrWeekStart();
        let endTime = this.getCurrWeekEnd();
        return [startTime, endTime];
    }

    /**
     * 获取本月开始时间
     * @returns {string}
     */
    getCurrMonthStart() {
        this.date.setDate(1);
        console.log(this.date);
        return this.timeFormat();
    }

    /**
     * 获取本月结束日期
     * @returns {string}
     */
    getCurrMonthEnd() {
        var monthFirstDate = new Date(this.year, this.month, 1);
        var oneDay = 1000 * 60 * 60 * 24;
        this.date = new Date(monthFirstDate - oneDay);
        console.log(this.date);
        return this.timeFormat();
    }

    /**
     * 获取本月日期范围
     * @returns {string[]}
     */
    getCurrMonthScope() {
        let startTime = this.getCurrMonthStart();
        let endTime = this.getCurrMonthEnd();
        return [startTime, endTime];
    }

    /**
     * 自定义加减天数
     * @param num
     * @returns {string}
     */
    computeTime(num) {
        this.date.setDate(this.date.getDate() + num);
        return this.timeFormat();
    }


}

相关文章

  • 自定义时间类

    js获取时间获取年、月、日、周格式化日期获取日期范围获取历史前N天的时间获取未来N天的时间

  • 实现一个自定义类加载器

    实现一个自定义类加载器,加载自定义目录下的类。 要读取的自定义目录的类 自定义ClassLoader 开始读取类 ...

  • java 类加载器

    自定义类加载器 自定义类加载器运行

  • @SentinelResource配置(中)

    客户自定义限流处理逻辑 创建CustomerBlockHandler类用于自定义限流处理逻辑 自定义限流处理类: ...

  • Java-面向对象

    核心思想:找适合的对象做适合的事情。 自定义类 自定义类的三步骤:1、自定义类class 类名 {// 事物的公共...

  • 自定义控件

    自定义多段选择器 类代码 测试 自定义按键Button 类代码 测试 自定义按钮2 首先创建自己按钮的类 应用自己...

  • ReactNative-自定义类和组件(八)

    一、自定义类 二、自定义组件

  • SpringBoot异常处理

    1. 配置全局异常和自定义异常 异常处理类(包括全局和自定义) 自定义异常类 2.返回自定义页面 创建自定义页面位...

  • 经典例题:模仿栈

    测试类: 栈类: 自定义异常: 添加用户类:

  • java多线程

    多线程的实现方式 自定义类继承thread类 重写run()方法,创建自定义类对象,调用start()方法...

网友评论

      本文标题:自定义时间类

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