MDN’s new design is in Beta! A sneak peek: https://blog.mozilla.org/opendesign/mdns-new-design-beta/

Date.prototype.toLocaleFormat()

현재 번역은 완벽하지 않습니다. 한국어로 문서 번역에 동참해주세요.

Non-standard
This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.

The non-standard toLocaleFormat() method converts a date to a string using the specified formatting. Intl.DateTimeFormat is an alternative to format dates in a standards-compliant way. See also the newer version of Date.prototype.toLocaleDateString().

문법

dateObj.toLocaleFormat(formatString)

매개변수

formatString
A format string in the same format expected by the strftime() function in C.

리턴값

A string representing the given date using the specified formatting.

설명

The toLocaleFormat() method provides greater software control over the formatting of the generated date and/or time. Names for months and days of the week are localized using the operating system's locale. However, ordering of the day and month and other localization tasks are not handled automatically since you have control over the order in which they occur. You should take care that the format string is localized properly according to the user's system settings. Be aware that the locale used is not necessarily the same as the locale of the browser.

Extension and XULRunner developers should know that just loading the format string from a .dtd or .properties file using a chrome://somedomain/locale/somefile.ext URI should be avoided, as the .dtd/.properties file and the toLocaleFormat() method does not not necessarily use the same locale, which could result in odd looking or even ambiguous or unreadable dates.

Also note that the behavior of the used locale depends on the platform, and the user might customize the locale used, so using the system locale the choose the format string might in some cases not even be adequate. You might consider using some of the more general toLocale* methods of the Date object or doing your own custom localization of the date to be displayed using some of the get* methods of the Date object instead of using this method.

예시

Using toLocaleFormat()

var today = new Date();
var date = today.toLocaleFormat('%A, %B %e, %Y'); // Bad example

이 예시에서, toLocaleFormat()은 "Wednesday, October 3, 2007"을 반환합니다. 이 예제의 서식 문자열은 제대로 지역화되어 있지 않기 때문에 위의 문제가 발생합니다.

폴리필

DateJS 라이브러리를 사용할 때, Date.prototype.toLocaleDateString()을 다음과 같이 폴리필할 수 있습니다.

if (!Date.prototype.toLocaleFormat) {
    (function() {
        Date.prototype.toLocaleFormat = function(formatString) {
            return this.format(formatString);
        };
    }());
}

Specifications

Not part of any specification. JavaScript 1.6에서 구현 됨.

브라우저 호환성

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support ? (Yes) No support ? ?
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support ? ? (Yes) ? ? ?

참고

문서 태그 및 공헌자

 이 페이지의 공헌자: Shin-JaeHeon
 최종 변경: Shin-JaeHeon,