关于前端:js时间戳日期转换实用工具

8次阅读

共计 3884 个字符,预计需要花费 10 分钟才能阅读完成。

发个之前写的日期工夫戳转换实用工具库,写这个工具的起因是因为之前工作的时候常常须要写工夫戳转换,因为无奈保障后端给的数据格式肯定是本人想要的,所以作为前端常常都得本人写工夫转换,索性就对立把常常见到的工夫转换需要整顿了一下写成了小工具,省下反复写代码的功夫。我的准则是前端能做的事就前端来解决,有功夫跟后端扯皮早就本人把货色做完了。大前端时代,前端能做到大部分以前后端能力做的事件,没有路就本人开路,能解决问题就好,无所谓前后端之争。

/**
 * 这是简便的工夫转换工具库。** 应用办法
 * import datetransform  from "common/datetransform.js";
 * 引入后可通过 datetransform.gethour(timestamp) 等办法间接应用
 *
 * 写了几个常见的办法:* toTformat(times)   // 将日期转换成含 T 的格局不含秒  如 2018-08-08T11:11
 * tonormalformat(times) // 将日期转换成规范格局
 * tostamp(times) 日期转为工夫戳   
 * tozerostamp(times) 日期转工夫戳 (零点)
 * getall(timestamp) 工夫戳转日期 (蕴含年月日时分秒)
 * getday(timestamp) 工夫戳转日期 (蕴含年月日)
 * gethour(timestamp) 工夫戳转日期 (蕴含时分秒)
 * getnow(type) 工夫戳转日期
 * // 以后日期 / 时分秒
 * type 为 "day" 时返回当天日期 (蕴含年月日) 
 * type 为 "hour" 时返回当天工夫 (蕴含时分秒) 
 * type 为空时返回当天日期加工夫 (蕴含年月日时分秒)
 * 
**/
    let datetransform = {
          // 将日期转换成含 T 的格局不含秒
    toTformat(times){
        var time=times
        if(time){var date = new Date(time);
        let Y = date.getFullYear() + '-'
        let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
        let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate());
        let h = (date.getHours() < 10 ? '0' +  date.getHours() :  date.getHours())  + ':'
        let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) 
        let s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds())
        return Y + M + D + "T" + h + m
    }else{return ""}
    },
        // 将日期转换成规范格局
    tonormalformat(times){
        var time=times
        if(time){var date = new Date(time);
        let Y = date.getFullYear() + '-'
        let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
        let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate());
        let h = (date.getHours() < 10 ? '0' +  date.getHours() :  date.getHours())  + ':'
        let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
        let s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds())
        return Y + M + D + " " + h + m + s
    }else{return ""}
    },
     // 日期转工夫戳
    tostamp(times) {if(times){let time = new Date(times)
        return time.getTime()}else{return ""}
    },
    // 日期转工夫戳 (零点)
    tozerostamp(times) {if(times){let time = new Date(times + "00:00:00")
        return time.getTime()}else{return ""} 
        
    },
    // 工夫戳转日期 (蕴含年月日时分秒)
    getall(timestamp) {if(timestamp){let date = new Date(parseInt(timestamp))
        let Y = date.getFullYear() + '-'
        let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
        let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate());
        let h = (date.getHours() < 10 ? '0' +  date.getHours() :  date.getHours())  + ':'
        let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
        let s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
        return Y + M + D + " " + h + m + s
    }else{return ""} 
    
    },
     // 工夫戳转日期 (蕴含年月日)
    getday(timestamp) {if(timestamp){let date = new Date(parseInt(timestamp))    
        let Y = date.getFullYear() + '-'
        let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
        let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate());
        let h = (date.getHours() < 10 ? '0' +  date.getHours() :  date.getHours())  + ':'
        let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
        let s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
        return Y + M + D
    }else{return ""} 
    },
     // 工夫戳转日期 (蕴含时分秒)
    gethour(timestamp) {if(timestamp){let date = new Date(parseInt(timestamp))
        let Y = date.getFullYear() + '-'
        let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
        let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate());
        let h = (date.getHours() < 10 ? '0' +  date.getHours() :  date.getHours())  + ':'
        let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
        let s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds())
        return h + m + s; 
    }else{return ""} 
    },
    // 日期 / 时分秒,参数为空时返回以后日期工夫 type 为 "day" 时返回当天日期 type 为 "hour" 时返回当天工夫 type 空时返回当天日期加工夫
    getnow(type) {let date = new Date()
        let Y = date.getFullYear() + '-'
        let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
        let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate());
        let h = (date.getHours() < 10 ? '0' +  date.getHours() :  date.getHours())  + ':'
        let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
        let s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
        if (type == "day") {return Y + M + D} else if (type == "hour") {return h + m + s;} else {return Y + M + D + " " + h + m + s}
    },
}

export default datetransform
正文完
 0