时间格式化函数

    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());

你可能感兴趣的:(前端javascript时间)