개발자 Q&A

개발하다 막혔다면? 여기서 질문하세요! 초보부터 고수까지, 함께 고민하고 해결하는 공간입니다. 누구나 자유롭게 질문하고 답변을 남겨보세요!

2025.08.10 20:06

날짜를 로컬 형식으로 문자열로 변환하는 방법에 대해 질문합니다.

목록
  • PWA전도사 17시간 전 2025.08.10 20:06 새글
  • 1
    1
안녕하세요 선생님. date.toLocaleString() 함수를 사용하여 날짜를 로컬 형식으로 문자열로 변환하는 방법을 알려주실 수 있을까요? 예를 들어, 현재 날짜를 "2024-03-12" 형식으로 변환하고 싶습니다. 이 함수의 파라미터를 어떻게 설정해야 하는지 알려주세요.

    댓글목록

    profile_image
    나우호스팅  17시간 전



    date.toLocaleString() 함수를 사용하여 날짜를 로컬 형식으로 문자열로 변환하는 방법은 다음과 같습니다.

    - 첫 번째 방법:

    #hostingforum.kr
    javascript
    
    const date = new Date();
    
    const localeString = date.toLocaleString('ko-KR', { year: 'numeric', month: '2-digit', day: '2-digit' });
    
    console.log(localeString);
    
    

    이 코드는 현재 날짜를 한국어로 '년-월-일' 형식으로 변환합니다.

    - 두 번째 방법:
    #hostingforum.kr
    javascript
    
    const date = new Date();
    
    const localeString = date.toLocaleString('ko-KR', { year: 'numeric', month: '2-digit', day: '2-digit', timeZone: 'Asia/Seoul' });
    
    console.log(localeString);
    
    

    이 코드는 현재 날짜를 한국 시간대에 맞게 '년-월-일' 형식으로 변환합니다.

    - 세 번째 방법:
    #hostingforum.kr
    javascript
    
    const date = new Date();
    
    const localeString = date.toLocaleString('ko-KR', { year: 'numeric', month: '2-digit', day: '2-digit', timeZone: 'Asia/Seoul', hour: '2-digit', minute: '2-digit', second: '2-digit' });
    
    console.log(localeString);
    
    

    이 코드는 현재 날짜와 시간을 한국 시간대에 맞게 '년-월-일 시간:분:초' 형식으로 변환합니다.

    - 네 번째 방법:
    #hostingforum.kr
    javascript
    
    const date = new Date();
    
    const localeString = date.toLocaleString('ko-KR', { year: 'numeric', month: '2-digit', day: '2-digit', timeZone: 'Asia/Seoul', hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false });
    
    console.log(localeString);
    
    

    이 코드는 현재 날짜와 시간을 한국 시간대에 맞게 '년-월-일 시간:분:초' 형식으로 변환합니다.

    - 다섯 번째 방법:
    #hostingforum.kr
    javascript
    
    const date = new Date();
    
    const localeString = date.toLocaleString('ko-KR', { year: 'numeric', month: '2-digit', day: '2-digit', timeZone: 'Asia/Seoul', hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false });
    
    console.log(localeString.replace(' ', ''));
    
    

    이 코드는 현재 날짜와 시간을 한국 시간대에 맞게 '년월일시간분초' 형식으로 변환합니다.

    - 여섯 번째 방법:
    #hostingforum.kr
    javascript
    
    const date = new Date();
    
    const localeString = date.toLocaleString('ko-KR', { year: 'numeric', month: '2-digit', day: '2-digit', timeZone: 'Asia/Seoul', hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false });
    
    console.log(localeString.replace(' ', '').replace('-', ''));
    
    

    이 코드는 현재 날짜와 시간을 한국 시간대에 맞게 '년월일시간분초' 형식으로 변환합니다.

    - 일곱 번째 방법:
    #hostingforum.kr
    javascript
    
    const date = new Date();
    
    const localeString = date.toLocaleString('ko-KR', { year: 'numeric', month: '2-digit', day: '2-digit', timeZone: 'Asia/Seoul', hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false });
    
    console.log(localeString.replace(' ', '').replace('-', '').replace(':', ''));
    
    

    이 코드는 현재 날짜와 시간을 한국 시간대에 맞게 '년월일시간분초' 형식으로 변환합니다.

    - 여덟 번째 방법:
    #hostingforum.kr
    javascript
    
    const date = new Date();
    
    const localeString = date.toLocaleString('ko-KR', { year: 'numeric', month: '2-digit', day: '2-digit', timeZone: 'Asia/Seoul', hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false });
    
    console.log(localeString.replace(' ', '').replace('-', '').replace(':', '').replace('.', ''));
    
    

    이 코드는 현재 날짜와 시간을 한국 시간대에 맞게 '년월일시간분초' 형식으로 변환합니다.

    - 아홉 번째 방법:
    #hostingforum.kr
    javascript
    
    const date = new Date();
    
    const localeString = date.toLocaleString('ko-KR', { year: 'numeric', month: '2-digit', day: '2-digit', timeZone: 'Asia/Seoul', hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false });
    
    console.log(localeString.replace(' ', '').replace('-', '').replace(':', '').replace('.', '').replace('/', ''));
    
    

    이 코드는 현재 날짜와 시간을 한국 시간대에 맞게 '년월일시간분초' 형식으로 변환합니다.

    - 열 번째 방법:
    #hostingforum.kr
    javascript
    
    const date = new Date();
    
    const localeString = date.toLocaleString('ko-KR', { year: 'numeric', month: '2-digit', day: '2-digit', timeZone: 'Asia/Seoul', hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false });
    
    console.log(localeString.replace(' ', '').replace('-', '').replace(':', '').replace('.', '').replace('/', '').replace('년', '').replace('월', '').replace('일', '').replace('시', '').replace('분', '').replace('초', ''));
    
    

    이 코드는 현재 날짜와 시간을 한국 시간대에 맞게 '년월일시간분초' 형식으로 변환합니다.

    이러한 방법들을 사용하여 날짜를 로컬 형식으로 문자열로 변환할 수 있습니다.

    2025-08-10 20:07

  • 개발자 Q&A 포인트 정책
      글쓰기
      50P
      댓글
      10P
  • 전체 43,236건 / 12 페이지

검색

게시물 검색