Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Date.prototype.toLocaleString()

To tłumaczenie jest niekompletne. Pomóż przetłumaczyć ten artykuł z języka angielskiego.

Podsumowanie

Konwertuje datę na łańcuch znakowy, używając aktualnej lokalnej konwersji.

Metoda toLocaleString polega na znalezieniu formatu daty w systemie operacyjnym. Konwertuje datę do łańcucha znakowego używając konwencji formatowania systemu operacyjnego, gdzie skrypt jest uruchomiony. Na przykład, w USA, miesiąc pojawia się przed dniem (04/15/98), gdzie w Niemczech data wyświetla miesiąc, zaraz po dniu (15.04.98). Jeśli system operacyjny nie używa, nie spełnia roku 2000 i nie używa pełnego roku dla lat przed 1900 lub po 2000, toLocaleString zwraca łańcuch znakowy, który nie jest uległy wobec roku 2000. toLocaleString postępuje podobnie do toString, kiedy konwertuje rok, który system operacyjny ma w niewłaściwym formacie.

Metody, takie jak getHours, getMinutes i getSeconds dają więcej przenośnych rezultatów niż toLocaleString

Składnia

dateObj.toLocaleString([locales[, options]])

Parametry

Check the Browser compatibility section to see which browsers support the locales and options arguments, and the Example: Checking for support for locales and options arguments for feature detection.

locales

Optional. A string with a BCP 47 language tag, or an array of such strings. For the general form and interpretation of the locales argument, see the Intl page. The following Unicode extension keys are allowed:

nu
Numbering system. Possible values include: "arab", "arabext", "bali", "beng", "deva", "fullwide", "gujr", "guru", "hanidec", "khmr", "knda", "laoo", "latn", "limb", "mlym", "mong", "mymr", "orya", "tamldec", "telu", "thai", "tibt".
ca
Calendar. Possible values include: "buddhist", "chinese", "coptic", "ethioaa", "ethiopic", "gregory", "hebrew", "indian", "islamic", "islamicc", "iso8601", "japanese", "persian", "roc".
options

Optional. An object with some or all of the following properties:

localeMatcher
The locale matching algorithm to use. Possible values are "lookup" and "best fit"; the default is "best fit". For information about this option, see the Intl page.
timeZone
The time zone to use. The only value implementations must recognize is "UTC"; the default is the runtime's default time zone. Implementations may also recognize the time zone names of the IANA time zone database, such as "Asia/Shanghai", "Asia/Kolkata", "America/New_York".
hour12
Whether to use 12-hour time (as opposed to 24-hour time). Possible values are true and false; the default is locale dependent.
formatMatcher
The format matching algorithm to use. Possible values are "basic" and "best fit"; the default is "best fit". See the following paragraphs for information about the use of this property.

The following properties describe the date-time components to use in formatted output, and their desired representations. Implementations are required to support at least the following subsets:

  • weekday, year, month, day, hour, minute, second
  • weekday, year, month, day
  • year, month, day
  • year, month
  • month, day
  • hour, minute, second
  • hour, minute

Implementations may support other subsets, and requests will be negotiated against all available subset-representation combinations to find the best match. Two algorithms are available for this negotiation and selected by the formatMatcher property: A fully specified "basic" algorithm and an implementation dependent "best fit" algorithm.

weekday
The representation of the weekday. Possible values are "narrow", "short", "long".
era
The representation of the era. Possible values are "narrow", "short", "long".
year
The representation of the year. Possible values are "numeric", "2-digit".
month
The representation of the month. Possible values are "numeric", "2-digit", "narrow", "short", "long".
day
The representation of the day. Possible values are "numeric", "2-digit".
hour
The representation of the hour. Possible values are "numeric", "2-digit".
minute
The representation of the minute. Possible values are "numeric", "2-digit".
second
The representation of the second. Possible values are "numeric", "2-digit".
timeZoneName
The representation of the time zone name. Possible values are "short", "long".

The default value for each date-time component property is undefined, but if the weekday, year, month, day, hour, minute, second properties are all undefined, then year, month, day, hour, minute, and second are assumed to be "numeric".

Przykłady

Zastosowanie toLocaleString()

W następującym przykładzie, obiekt today jest Date:

var today = new Date(95,11,18,17,28,35); //miesiące są reprezentowane poprzez liczby od 0 do 11
today.toLocaleString(); // 12/18/95 17:28:35

W tym przykładzie, toLocaleString zwraca łańcuch znaków, który jest podobny do następującej formy. Dokładnie to taki format, jaki jest w systemie.

Zobacz także

Autorzy i etykiety dokumentu

 Autorzy tej strony: teoli, Ptak82
 Ostatnia aktualizacja: teoli,