js date的封装 时间戳,字符串,date,json转换

class Time {

constructor(){
}

/**
 *   1.1 时间对象转字符串
 *        date  --->  2019-03-08 12:12:12
 * **/

timeToString( date, format = "yyyy-MM-dd HH:mm:ss" ){
    if ( !date ){
        return '';
    }

    //将日期数据,替换成format
    let year   = date.getFullYear();
    let month  = date.getMonth()+1;
    let day    = date.getDate();
    let hour   = date.getHours();
    let minute = date.getMinutes();
    let second = date.getSeconds();
    format = format.replace( /yyyy|yy|YYYY|yy/, year );
    format = format.replace( "MM", month>9 ? month :( '0'+month ) );
    format = format.replace( "dd", day>9 ? day : ('0'+day) );
    format = format.replace( "HH", hour>9? hour : ('0' + hour) );
    format = format.replace( "mm", minute>9 ? minute : ('0'+minute));
    format = format.replace( "ss", second>9? second : ('0'+second));
    return format;
}

/**
 *  1.2 日期转字符串形式
 *       比如:  Date  --> 2019-03-02
 * */
dateToString( date, format = "yyyy-MM-dd" ){
    if ( !date ){
        return '';
    }
    //将日期数据,替换成format
    let year   = date.getFullYear();
    let month  = date.getMonth()+1;
    let day    = date.getDate();
    format = format.replace( /yyyy|yy|YYYY|yy/, year );
    format = format.replace( "MM", month>9 ? month :( '0'+month ) );
    format = format.replace( "dd", day>9 ? day : ('0'+day) );
    return format;
}


/**
 * 1.3 获取当前时间的字符串形式
 *     比如:  2019-03-08 12:12:12
 * */
currentTimeToFormat(  format = "yyyy-MM-dd HH:mm:ss" ){
    return this.getTimeString(new Date(), format);
}

/**
 * 1.4 获取当前日期的字符串形式
 *      比如  2019-03-02
 * */
currentDateToFormat( format = "yyyy-MM-dd"  ){
    return this.getDateString(new Date(), format);
}


/**
 *  1.5 日期字符串转date对象
 *      比如: 2019-03-08  --> date
 *      注意:如果直接打印date,此时是去除了时区的时间。
 * */
fromatToDate( dateString, format="yyyy-MM-dd HH:mm:ss" ){
    if( !dateString ){
        return null;
    }
    dateString = dateString.replace(/\W/g, '-').replace(/-+/g, '-');
    return new Date( new Date(dateString).getTime()+new Date().getTimezoneOffset()*1000*60 )
}


/**
 *  1.6 时间字符串形式的时间 转换成 date
 *      比如: 2019-03-08 12:12:12 --> date
 *      注意:如果直接打印date,此时是去除了时区的时间。
 * */
formatToTime( dateString, format="yyyy-MM-dd HH:mm:ss" ){
    if( !dateString ){
        return null;
    }
    const list = dateString.replace(/\W/g, ':').replace(/:+/g, ':').split(":");
    let stringBuild = list.slice(0,3).join("/") +" "+ list.slice(3,6).join(":")
    return new Date( stringBuild );
}


/**
 *  2.1 Date对象,转成js对象
 * */
dateToObject( date ){

    if ( !date ){
        return {};
    }

    return {
        year : date.getFullYear(),
        month: date.getMonth()+1,
        day: date.getDate(),
        hour: date.getHours(),
        minute: date.getMinutes(),
        seconds: date.getSeconds()
    }
}


/**
 *  2.2 时间字符串,转换成js对象
 * */
objectToDate( timeObject ){

    let stringBuild = "";
    stringBuild += (timeObject.year? timeObject.year : '1970')  +"/"
    stringBuild += (timeObject.month?timeObject.month : '00') + "/"
    stringBuild += (timeObject.day?timeObject.day : '00') + " "
    stringBuild += (timeObject.hour?timeObject.hour: '00') + ":"
    stringBuild += (timeObject.minute?timeObject.minute:'00')+":"
    stringBuild += (timeObject.seconds?timeObject.seconds:'00')
    return new Date(stringBuild);
}


/**
 *  时间戳  转  Date
 * */
timestampToDate( timestamp ){
   return new Date(timestamp);
}

/**
 *  时间戳  转 format
 * */
timestampToFormat( timestamp, format="yyyy-MM-dd HH:mm:ss" ){
    const date = this.timestampToDate(timestamp);
    return this.currentDateToFormat( date, format );
}

/**
 *  时间戳 转 JSON对象
 * */
timestampToJSON( timestamp ){
    const date = this.timestampToDate(timestamp);
    return this.dateToObject(date);
}

/**
 *  date 转 时间戳
 * */
dateToTimestamp( date ){
    return  date.getTime()/1000;
}

}

module.exports = new Time();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值