js 获取当前日期时间

时间:2022-09-30 19:24:06
function getCurrentDate(fulldate = false, separator = ['-', ':']) {
    let currentTimeObj = new Date();

    let year = currentTimeObj.getFullYear();
    let month = (currentTimeObj.getMonth() + 1);
    month = month >= 10 ? month : '0' + month;
    let day = currentTimeObj.getDate();
    day = day >= 10 ? day : '0' + day;
    let hour = currentTimeObj.getHours();
    hour = hour >= 10 ? hour : '0' + hour;
    let minute = currentTimeObj.getMinutes();
    minute = minute >= 10 ? minute : '0' + minute;
    let second = currentTimeObj.getSeconds();
    second = second >= 10 ? second : '0' + second;
    
    if (fulldate) {
        let ymd = [year, month, day];
        let his = [hour, minute, second];
        return ymd.join(separator[0]) + ' ' + his.join(separator[1]);
    } else {
        let ymd = [year, month, day];
        return ymd.join(separator[0]);
    }
}