js获取当前具体时间

    /**
     * 获取当前时间
     * @param isTime  true:显示日期和时间,如:2018-09-20 13:25:12;false:显示日期,如:2018-09-20。
     * @returns {string}
     */
    function getCurentTime(isTime=true) {
        var now = new Date();
        var clock = "";
        var year = now.getFullYear();       //
        clock += year + "-";
        var month = now.getMonth() + 1;     //
        if (month < 10) {
            clock += "0";
        }
        clock += month + "-";
        var day = now.getDate();            //
        if (day < 10) {
            clock += "0";
        }
        if (isTime == true) {
            clock += day + " ";
            var hh = now.getHours();            //
            if (hh < 10) {
                clock += "0";
            }

            clock += hh + ":";
            var mm = now.getMinutes();     //
            if (mm < 10) {
                clock += '0';
            }
            clock += mm + ":";
            var ss = now.getSeconds();     //
            if (ss < 10) {
                clock += '0';
            }
            clock += ss;
        }
        if (isTime == false) {
            clock += day;
        }
        return clock;
    }
posted @ 2018-10-15 13:29  样子2018  阅读(1066)  评论(2编辑  收藏  举报