toString() 메서드는 Date 객체의 시간을 문자열로 반환합니다.
구문
dateObj.toString()
반환 값
주어진 날짜를 나타내는 문자열.
설명
Date 객체는 toString() 메서드를 Object.prototype이 아닌 Date.prototype에서 상속받습니다. Date.prototype.toString()의 반환값은 ECMA-262에 명시되어있으며 다음과 같이 요약할 수 있습니다.
- 요일: 세 글자 영어 요일명. 예: "Sat"
- 공백
- 월: 세 글자 영어 월 이름. 예: "Sep"
- 공백
- 일: 두 글자 숫자. 예: "01"
- 공백
- 연: 네 글자 숫자. 예: "2018"
- 공백
- 시: 두 글자 숫자. 예: "14"
- 콜론
- 분: 두 글자 숫자. 예: "53"
- 콜론
- 초: 두 글자 숫자. 예: "26"
- 공백
- 문자열 "GMT"
- 시간대 차이의 부호.
- 0 이상일 경우 "+"
- 0 미만일 경우 "-"
- 두 글자 숫자로 표현한 시 차이. 예: "14"
- 두 글자 숫자로 표현한 분 차이. 예: "00"
- 선택사항: 다음으로 구성한 시간대의 이름.
- 공백
- 여는 소괄호
- 구현마다 다를 수 있는 시간대 이름. 약어와 전체 이름 둘 다 가능. 예: "Korea Standard Time", "한국 표준시" 또는 "KST"
- 닫는 소괄호
예: "Sat Sep 01 2018 14:53:26 GMT+0900 (KST)"
ECMAScript 2018(제9판) 전까지 toString()의 반환 형식은 구현에 따라 다를 수 있었습니다. 따라서 특정 형식에 의존하지 않아야 합니다.
toString() 메서드는 날짜를 문자열로 표현해야 할 때 자동으로 쓰입니다. console.log(new Date())와 const today = 'Today is ' + new Date() 등의 경우를 예로 들 수 있습니다.
toString()은 제네릭 메서드로 this의 값이 Date일 필요는 없습니다. 그러나 내부적으로 [[TimeValue]] 속성이 필요한데, 이는 JavaScript로는 설정할 수 없어 결국 사용이 Date 인스턴스로 제한됩니다. Date 외의 다른 객체에서 호출하면 TypeError가 발생합니다.
예제
toString() 사용
var x = new Date();
var myVar = x.toString(); // assigns a string value to myVar in the same format as:
// Mon Sep 08 1998 14:36:22 GMT+0900 (KST)
명세
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 1st Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.0. |
| ECMAScript 5.1 (ECMA-262) The definition of 'Date.prototype.toLocaleTimeString' in that specification. |
Standard | |
| ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Date.prototype.toString' in that specification. |
Standard | |
| ECMAScript Latest Draft (ECMA-262) The definition of 'Date.prototype.toString' in that specification. |
Draft |
브라우저 호환성
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
toString | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
Legend
- Full support
- Full support