본문 바로가기

HTML CSS JAVASCRIPT

자바스크립트 간단한 코드로 현재 시간 출력하기 (옵션 다양함)

300x250

자바스크립트로 현재 시간을 출력하는 방법 중 가장 일반적인 방법인 Date 객체를 사용하는 것 말고

지역 설정에 맞는 시간 형식으로 현재 시간을 출력할 수 있습니다.

 

 

아래가 그 결과입니다.

 

 

자세한 설명은 이곳에

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat

 

Intl.DateTimeFormat - JavaScript | MDN

Intl.DateTimeFormat 은 언어에 맞는 날짜 및 시간 서식을 적용하기 위한 객체입니다.

developer.mozilla.org

 

아래는 전체 코드입니다.

const KoreanFormatter = new Intl.DateTimeFormat('ko-KR', {
	//numeric: 숫자형식 / long: 전체 이름 / narrow: 가장 짧은 형식
    year: 'numeric',
    month: 'long',
    day: 'numeric',
    weekday: 'narrow',
    hour: 'numeric',
    minute: 'numeric',
    second: 'numeric',
  });



setInterval(() => {
	const nowTime = KoreanFormatter.format(new Date());
	document.body.innerText = '';
	document.body.innerText = nowTime;
}, 1000);

 

(광고 클릭은 제작에 큰 힘이 됩니다.)

 

300x250