js 获取当前时间yyy-mm-dd hh:mm:ss

直接在控制台运行 输出当前时间2023-07-13 09:51:15

let time = new Date().toLocaleString().split('/');
  let time1 = time[1] > 10 ? time[1] : 0 + time[1];
  let time2 = time[2].split(' ');
  let time3 = time2[0] > 10 ? time2[0] : 0 + time2[0];
  let date = time[0] + '-' + time1 + '-' + time3 + ' ' + time2[1];
  console.log(date); // 2023-07-13 09:51:15