JavaScript、typescript、nodejs日期操作-笔记

JavaScript、typescript、nodejs日期操作:
1、获取当前日期。
2、格式化日期。
3、获取当前时间-分钟。
4、获取当日期格式化至分钟。
5、传入日期增加小时,并格式化为2017092215。
6、传入日期增加小时,并获取当前小时。
7、传入日期增加天数,并格式化为20170922。
8、格式化到天。
9、格式化到小时。
10、获取当前小时。
11、添加日。
12、日期比较。
注意:getMonth()获取到的日期差一个月,最好+1
date-utils
typescript BaseDate类不断完善中,最后更新日期2017-09-22
/**
* 日期处理公共类
*
* @export
* @class BaseDate
*/
export class BaseDate {
    /**
     * 获取当前日期:20170718
     *
     * @returns {string}
     * @memberof BaseDate
     */
    public static getDateNow () : string {
        let dateNow = new Date ();
        let year : number = dateNow . getFullYear ();
        let month : string | number = dateNow . getMonth () + 1 ;
        let day : string | number = dateNow . getDate ();
        //return new Date(year, month, day);
        if ( month < 10 ) {
            month = "0" + month ;
        }
        if ( day < 10 ) {
            day = "0" + day ;
        }
        return year + "" + month + "" + day ;
    }

    /**
     * 格式化日期 格式化后:20170718
     *
     * @param {Date} date
     * @returns {string}
     * @memberof BaseDate
     */
    public static formatDate ( date : Date ) : string {
        var dateNow = date ;
        var year : number = dateNow . getFullYear ();
        var month : string | number = dateNow . getMonth () + 1 ;
        var day : string | number = dateNow . getDate ();
        //return new Date(year, month, day);
        if ( month < 10 ) {
            month = "0" + month ;
        }
        if ( day < 10 ) {
            day = "0" + day ;
        }
        return year + "" + month + "" + day ;
        //return new Date("yyyy-MM-dd");
    }

    /**
     * 获取当前时间-分钟
     *
     * @static
     * @returns {string}
     * @memberof BaseDate
     */
    public static getCurrentMinutesTostring () : string {
        let date = new Date ();
        let minute : string = date . getMinutes () < 10 ? "0" + date . getMinutes () : date . getMinutes (). toString ();
        return minute ;
    }

    /**
     * 获取当日期格式化至小时
     *
     * @static
     * @returns {string}
     * @memberof BaseDate
     */
    public static getCurrentDateToString_ss () : string {
        let date = new Date ();
        let year : number = date . getFullYear ();
        let month : string | number = date . getMonth () + 1 ;
        let day : string | number = date . getDate ();
        let hour : string | number = date . getHours () < 10 ? "0" + date . getHours () : date . getHours ();
        let minute : string | number = date . getMinutes () < 10 ? "0" + date . getMinutes () : date . getMinutes ();
        //return new Date(year, month, day);
        if ( month < 10 ) {
            month = "0" + month ;
        }
        if ( day < 10 ) {
            day = "0" + day ;
        }
        return year + "" + month + "" + day + ":" + hour ; // + "" + minute;
    }


    /**
     * 获取当前时间的前一个小时
     *
     * @static
     * @param {number} addHours
     * @returns {string}
     * @memberof BaseDate
     */
    public static getAddHour ( date : Date , hour : number ) : string {
        let date_hour = new Date ( date . setHours ( date . getHours () + hour ));
        return this . FormatTohh ( date_hour );
        //return new Date(date.setDate(date.getDate() + days));
    }
    /**
    * 获取当前时间的前一个小时
    *
    * @static
    * @param {number} addHours
    * @returns {string}
    * @memberof BaseDate
    */
    public   static getAddHours ( date : Date , hour : number ) {
        let date_hour = new Date ( date . setHours ( date . getHours () + hour ));
        return date . getHours ();
        //return new Date(date.setDate(date.getDate() + days));
    }

    /**
    * 获取当前时间的前一天
    *
    * @static
    * @param {number} addHours
    * @returns {string}
    * @memberof BaseDate
    */
    public static getAddDay ( date : Date , day : number ) {
        let date_day = new Date ( date . setDate ( date . getDate () + day ));
        return this . FormatToDay ( date_day );
        //return new Date(date.setDate(date.getDate() + days));
    }

    /**
    * 格式化到小时
    *
    * @static
    * @param {Date} date
    * @returns
    * @memberof BaseDate
    */
    public static FormatToDay ( date : Date ) {
        let year = date . getFullYear ();
        let month : string | number = date . getMonth () + 1 ;
        let day : string | number = date . getDate ();
        // let hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
        //let minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
        //return new Date(year, month, day);
        if ( month < 10 ) {
            month = "0" + month ;
        }
        if ( day < 10 ) {
            day = "0" + day ;
        }
        return year + "" + month + "" + day ; // + ":" + hour; // + "" + minute;
    }

    /**
     * 格式化到小时
     *
     * @static
     * @param {Date} date
     * @returns
     * @memberof BaseDate
     */
    public static FormatTohh ( date : Date ) {

        let year : number = date . getFullYear ();
        let month : string | number = date . getMonth () + 1 ;
        let day : string | number = date . getDate ();
        let hour : string | number = date . getHours () < 10 ? "0" + date . getHours () : date . getHours ();
        let minute : string | number = date . getMinutes () < 10 ? "0" + date . getMinutes () : date . getMinutes ();
        //return new Date(year, month, day);
        if ( month < 10 ) {
            month = "0" + month ;
        }
        if ( day < 10 ) {
            day = "0" + day ;
        }
        return year + "" + month + "" + day + ":" + hour ; // + "" + minute;
    }

    /**
     * 获取当前小时
     *
     * @static
     * @returns {string}
     * @memberof BaseDate
     */
    public static getCurrentHour () : string {
        let date = new Date ();
        return date . getHours () < 10 ? "0" + date . getHours () : date . getHours () + "" ;
    }

    /**
     *  /**
     * 添加日
     *
     * @param {Date} date 日期
     * @param {number} days 添加的天数    
     * @returns {Date}
     * @memberof BaseDate
     */
    public static addDays ( date : Date , days : number ) : Date {
        return new Date ( date . setDate ( date . getDate () + days ));
    }
}


            //字符串格式化为日期2016/09/08
             function   formatStringToDate ( valueDate ) {
                 //console.log("formatStringToDate:" + valueDate.replace(/-/g, "/"));
                 return   new   Date ( Date . parse ( valueDate . replace ( /-/ g "/" )));
            }
            //获取当前日期
             function   getDateNow () {
                 var   dateNow  =  new   Date ();
                 var   year  =  dateNow . getFullYear ();
                 var   month  =  dateNow . getMonth ();
                 var   day  =  dateNow . getDate ();
                 return   new   Date ( year month day );
                 //return year + "-" + month + "-" + day;
                 //return new Date("yyyy-MM-dd");
            }

             //为当前日期添加月份
             function   addMonths ( date month ) {
                 var   dateNow  =  date ;
                 var   year  =  dateNow . getFullYear ();
                 var   month  =  dateNow . getMonth () +  month ;
                 var   day  =  dateNow . getDate ();
                 if  ( parseInt ( month  /  12 ) >  0 ) {
                     year  +=  parseInt ( month  /  12 )
                }
                 //console.log("year:" + year + " month:" + month + " day:" + day);
                 return   new   Date ( year month day );

            }

             //添加日
             function   addDays ( date days ) {
                 return   new   Date ( date . setDate ( date . getDate () +  days ));
            }

             //是否超过时限,超过返回true,没有是false
             function   isDateLimit ( indexDate limitDate ) {
                 //console.log("【indexDate:】" + indexDate + " 【limitDate:】" + limitDate);
                 //console.log(indexDate <= limitDate ? true : false);
                 return   indexDate  <=  limitDate  ?  true  :  false ;
            }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值