js 获取当前日期时间
Posted 宋健安
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js 获取当前日期时间相关的知识,希望对你有一定的参考价值。
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]); } }
以上是关于js 获取当前日期时间的主要内容,如果未能解决你的问题,请参考以下文章