Skip to content
날짜 형식을기술 정리

A regex to change a date like 20231228 into 2023-12-28

1 min readTech notesMoved from my Tistory blogRead the Korean original

While writing code, the date data coming from the backend was in a format like date : '20231228', and I wanted it in a format like '2023-12-28'. Along the way, I found an easy way to do it.

js
const testDate = '20231228';

console.log(testDate.replace(/(\d{4})(\d{2})(\d{2})/, '$1-$2-$3'));

As in the code above, you can change it easily with replace(/(\d {4})(\d {2})(\d {2})/, '$1-$2-$3' ).

  • #JS
  • #date format conversion
  • #JavaScript
  • #regex

More in Tech notes