js日期格式化,输入20230812,输出2023-08-12

技术 · 2023-10-17
js日期格式化,输入20230812,输出2023-08-12

js日期格式化,输入20230812,输出2023-08-12

// 方法一:
function getAdjustTime(timeStr) {
  return timeStr.replace(/(\d{4})(\d{2})(\d{2})/, '$1-$2-$3');
}
console.log(getAdjustTime("20190416")); //2019-04-16

// 方法二:
function fomateDate(date) {
  let dateArr = date.split("").map((item, index) => {
    if (index == 3 || index == 5) {
      item = item + "-";
    }
    return item;
  });
  return dateArr.join("");
}
console.log(fomateDate("20231123")); //2023-11-23
javascript
  1. 感谢分享

Theme Jasmine by Kent Liao