格式化後的指定格式的日期和時間,封裝一個函數 function getDate() { var dt = new Date(); var year = dt.getFullYear(); var month = dt.getMonth(); var date = dt.getDate(); var ...
格式化後的指定格式的日期和時間,封裝一個函數
function getDate() { var dt = new Date(); var year = dt.getFullYear(); var month = dt.getMonth(); var date = dt.getDate(); var hour = dt.getHours(); var minute = dt.getMinutes(); var second = dt.getSeconds(); month = month < 10 ? "0" + month : month; date = date < 10 ? "0" + date : date; hour = hour < 10 ? "0" + hour : hour; minute = minute < 10 ? "0" + hour : hour; second = second < 10 ? "0" + second : second; return year + "年" + month + "月" + date + "日" + hour + ":" + minute + ":" + second; } console.log(getDate(new Date()));