关于前端:时间格式化函数

48次阅读

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

    function timeFormat(time) {var date = time == undefined ? new Date() : new Date(time);
        var seperator1 = "-";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {month = "0" + month;}
        if (strDate >= 0 && strDate <= 9) {strDate = "0" + strDate;}
        var currentdate = year + seperator1 + month + seperator1 + strDate;
        // HH:mm:ss
        var HH = date.getHours();
        var mm = date.getMinutes();
        var ss = date.getSeconds();
        HH = (HH <= 9) ? ("0" + HH) : HH
        mm = (mm <= 9) ? ("0" + mm) : mm
        ss = (ss <= 9) ? ("0" + ss) : ss
        var seperator2 = ":";
        currentdate += ' ' + HH + seperator2 + mm + seperator2 + ss;
        return currentdate;
        //  return strDate;
    }
    //document.querySelector("#app").setAttribute("data-version", timeFormat());

正文完
 0