logo

JavaScript에서 날짜 형식을 지정하는 방법은 무엇입니까?

이 기사에서는 JavaScript를 사용하여 Date 객체를 다양한 형식의 다양한 날짜 문자열로 형식화하는 다양한 방법에 대해 알아봅니다.

JavaScript를 사용하여 Date 객체를 다양한 형식의 다양한 날짜 문자열로 형식화하는 방법을 살펴보겠습니다. 웹 애플리케이션을 구축하든, 데이터를 조작하든, 단순히 날짜를 표시하든 관계없이 이러한 기술을 익히면 필요에 가장 적합한 형식으로 날짜를 표시할 수 있습니다. JavaScript에서 날짜 형식의 다양성을 살펴보겠습니다.

아래 목록에는 JavaScript에서 날짜 형식을 지정하는 다양한 방법이 포함되어 있습니다.



내용의 테이블

방법 1: toDateString() 메서드 사용

그만큼 toDateString() 메서드 날짜 객체를 사람이 읽을 수 있는 형식으로 형식화합니다. 일 월 일 연도.

통사론:

dateObj.toDateString();>

예: 아래 코드 예제에서는 날짜 형식을 지정하기 위해 toDateString() 메서드를 사용하는 방법을 설명합니다.

자바스크립트
const currentDate = new Date(); const formattedDate = currentDate.toDateString(); console.log(formattedDate);>

산출
Fri Dec 29 2023>

방법 2: toISOString() 메서드 사용

그만큼 toISOString() 메서드 ISO 8601 형식을 따르는 국제 표준으로 데이터 형식을 지정합니다.

통사론:

dateObj.toISOString();>

예: 아래 코드 예제에서는 toISOString() 메서드를 구현하여 날짜 객체의 형식을 지정합니다.

자바스크립트
const currentDate = new Date(); const formattedDate = currentDate.toISOString(); console.log(formattedDate);>

산출
2023-12-29T09:39:27.634Z>

방법 3: toLocaleDateString() 메서드 사용

toLocaleDateString() 메서드 날짜 객체의 날짜 부분을 시스템에서 가져온 것과 동일한 형식이나 지정된 형식으로 형식화합니다.

통사론:

dateObj.toLocaleDateString();

예: 아래 코드 예제는 날짜를 시스템에서 가져온 형식으로 변환합니다.

자바스크립트
const currentDate = new Date(); const formattedLocalDate = currentDate.toLocaleDateString(); const formattedInSpecifiedFormat = currentDate.toLocaleDateString("hi-IN"); console.log(formattedLocalDate); console.log(formattedInSpecifiedFormat);>

산출
12/29/2023 29/12/2023>

방법 4: toLocaleString() 메서드 사용

toLocaleString() 메서드 toLocaleDateString()이 작동하는 것과 비슷한 방식으로 작동합니다. 유일한 차이점은 형식이 지정된 문자열로 시간도 반환한다는 것입니다.

통사론:

dateObj.toLocaleString();

예: 아래 예제에서는 toLocaleString() 메서드를 사용하여 날짜 형식을 지정합니다.

자바스크립트
const currentDate = new Date(); const formattedLocalDate = currentDate.toLocaleString(); const formattedInSpecifiedFormat = currentDate.toLocaleString("hi-IN"); console.log(formattedLocalDate); console.log(formattedInSpecifiedFormat);>

산출
12/29/2023, 9:39:27 AM 29/12/2023, 9:39:27 am>

방법 5: Intl.DateTimeFormat() 개체 메서드 사용

Intl.DateTimeFormat() 객체 메서드 날짜 객체의 형식을 지정하는 강력한 객체 방법입니다. 날짜와 시간 형식을 지정하기 위해 지정된 옵션을 사용하여 날짜를 지정된 형식으로 형식화합니다.

통사론:

const formatObj = new Intl.DateTimeFormat('en-US'); formatObj.format(dateObj);>

예: 아래 예제에서는 intl.DateTimeFormat() 객체 메서드를 사용하여 날짜 객체의 형식을 지정합니다.

자바스크립트
const currentDate = new Date(); const dateTimeFormatter = new Intl.DateTimeFormat("en-US", {dateStyle: 'long'}); const formattedDate = dateTimeFormatter.format(currentDate); console.log(formattedDate);>

산출
December 29, 2023>

방법 6: Date 메서드를 사용하여 수동으로 날짜 형식 지정

이 접근 방식에서는 다양한 날짜 메서드를 사용하여 날짜 개체의 일, 날짜, 월, 연도를 가져온 다음 이를 연결하여 형식이 지정된 날짜 문자열을 형성합니다.

예: 아래 코드 예제에서는 다양한 날짜 메서드를 사용하여 날짜를 가져오고 형식을 지정합니다.

자바스크립트
const weekDays =  ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; const monthsArr =  ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; const currentDateObj = new Date(); const currentDay = weekDays[currentDateObj.getDay()]; const currentDate = currentDateObj.getDate(); const currentMonth = monthsArr[currentDateObj.getMonth()]; const currentYear = currentDateObj.getFullYear(); console.log(`${currentDay} ${currentDate} ${currentMonth}, ${currentYear}`);>

산출
Fri 29 Dec, 2023>