Encapsulation for converting the timestamp to other time formats

Recently I did a vue-related project, and used the time returned by the background, because the time returned by the background is in the time stamp format, and the display of the time in different formats is used multiple times in the project, so I encapsulated the time stamp conversion time by myself. The format method may need to be optimized, but it is understandable for novices, and I hope you guys can correct the deficiencies.
Closer to home, the encapsulation method considers the issue of parameters. According to the parameter execution method, directly upload the code. If you don't understand, you can read the comments.

<script>
    /*
    *@param {时间戳} timestamp
    *@param {转换的格式} typedef
    *@param {连接符} concat
    */
    //时间格式的封装  如将2019-5-6 11:8:34变成2019/5/6 11:08:34或者变成2019-5等等你想要的时间格式 
    function formatTime(timestamp, typedef = "YY|MM|DD", concat = ".") {
    
    
        let time
        if (typeof timestamp === "string") {
    
    
            time = Number(timestamp)
        } else {
    
    
            time = timestamp
        }
        if (time) {
    
    
            let data = new Date(time)
            let Year = data.getFullYear()    //获取年
            let Month = data.getMonth() + 1  //获取月
            let Day = data.getDate()         //获取日
            let Hour = data.getHours()       //获取时
            let Min = data.getMinutes()      //获取分
            let Secon = data.getSeconds()    //获取秒
            //不大于10补零操作
            Month = add0(Month)
            Day = add0(Day)
            Hour = add0(Hour)
            Min = add0(Min)
            Secon = add0(Secon)

            let newTime = ''
            if (typedef === "YY|MM|DD HH:MM:SS") {
    
    
                newTime = '' + Year + concat + Month + concat + Day + ' ' + Hour + ':' + Min + ':' + Secon
            } else if (typedef === "YY|MM|DD HH:MM") {
    
    
                newTime = '' + Year + concat + Month + concat + Day + '' + Hour + ':' + Min
            } else if (typedef === "YY|MM|DD") {
    
    
                newTime = '' + Year + concat + Month + concat + Day
            } else if (typedef === "YY|MM") {
    
    
                newTime = '' + Year + concat + Month
            } else if (typedef === "MM|DD") {
    
    
                newTime = '' + Month + concat + Day
            } else if (typedef === "YY") {
    
    
                newTime = '' + Year
            } else if (typedef === "MM") {
    
    
                newTime = '' + Month
            } else if (typedef === "DD") {
    
    
                newTime = '' + Day
            }
            return newTime
        }
    }
    //补零
    function add0(time) {
    
    
        return time > 10 ? time : '0' + time
    }
    //检验代码
    console.log(formatTime("1564804053345", "YY|MM|DD HH:MM:SS", "-"))   //2019-08-03 11:47:33
    console.log(formatTime("1564804053345", "YY|MM", "/"))              //2019/08
    console.log(formatTime(1564804053345, "YY|MM", "-"))               //2019-08
</script>

When using, just call the method with reference to the parameter format (before calling, make sure that the time format you pass in is a timestamp, which can be a string or a number (milliseconds)) and then you can convert it to the time format you want ! In the vue project, it is directly in main.js, and then imported into the global, you can use it anywhere.

end

Well, if you find it useful, please comment 1. We can communicate and learn together if we don’t understand.

Guess you like

Origin blog.csdn.net/Smell_rookie/article/details/98334455