JavaScript獲取當前時間且格式爲yyyy-mm-dd hh:mm:ss

//獲取當前時間
            function getNowFormatDate() {
                var date = new Date();
                var seperator1 = "-";
                var seperator2 = ":";
                var month = getNewDate(date.getMonth() + 1);
                var day = getNewDate(date.getDate());
                var hours = getNewDate(date.getHours());
                var minutes = getNewDate(date.getMinutes());
                var seconds = getNewDate(date.getSeconds());
                //統一格式爲兩位數
                function getNewDate(date) {
                    if (date <= 9) {
                        date = "0" + date;
                    }
                    return date;
                }

                var currentDate = date.getFullYear() + seperator1 + month + seperator1 + day
                    + " " + hours + seperator2 + minutes + seperator2 + seconds;
                return currentDate;
            }
相關文章
相關標籤/搜索