Intl.RelativeTimeFormat
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itโs been available across browsers since โจ2020๋ 9์โฉ.
Intl.RelativeTimeFormat
์ ์ธ์ด์ ์์กด์ ์ธ ์๋ ์๊ฐ ํ์์ ์ฌ์ฉํ ์ ์๊ฒ ํฉ๋๋ค.
์๋ํด ๋ณด๊ธฐ
const rtf1 = new Intl.RelativeTimeFormat("en", { style: "short" });
console.log(rtf1.format(3, "quarter"));
// Expected output: "in 3 qtrs."
console.log(rtf1.format(-1, "day"));
// Expected output: "1 day ago"
const rtf2 = new Intl.RelativeTimeFormat("es", { numeric: "auto" });
console.log(rtf2.format(2, "day"));
// Expected output: "pasado maรฑana"
์์ฑ์
Intl.RelativeTimeFormat()
-
์๋ก์ด
Intl.RelativeTimeFormat
๊ฐ์ฒด๋ฅผ ์์ฑํฉ๋๋ค.
์ ์ ๋ฉ์๋
Intl.RelativeTimeFormat.supportedLocalesOf()
-
์คํจ์ ๋ฐํ์์ ๊ธฐ๋ณธ ๋ก์ผ์ผ๋ก ๋์๊ฐ์ง ์์๋ ๋๋ ๋ก์ผ์ผ์ ๋ฐฐ์ด์ ๋ฐํํฉ๋๋ค. ์ง์ ๋ ๋ก์ผ์ผ ์ค, ์คํ์์ ๋ํดํธ์ ๋ก์ผ์ผ์ ํด๋ฐฑ ํ์ง ์๊ณ ๋์๋๋ ๊ฒ์ ๋ฐฐ์ด์ ๋ฃ์ด ๋๋ ค์ค๋๋ค.
์ธ์คํด์ค ์์ฑ
์๋ ์์ฑ์ Intl.RelativeTimeFormat.prototype
์ ์ ์๋์ด ์์ผ๋ฉฐ ๋ชจ๋ Intl.RelativeTimeFormat
์ธ์คํด์ค๊ฐ ๊ณต์ ํฉ๋๋ค.
Intl.RelativeTimeFormat.prototype.constructor
-
์ธ์คํด์ค ๊ฐ์ฒด๋ฅผ ์์ฑํ ์์ฑ์ ํจ์์ ๋๋ค.
Intl.RelativeTimeFormat
์ธ์คํด์ค์ ๊ฒฝ์ฐ, ์ด๊ธฐ ๊ฐ์Intl.RelativeTimeFormat
์์ฑ์์ ๋๋ค. Intl.RelativeTimeFormat.prototype[@@toStringTag]
-
@@toStringTag
์์ฑ์ ์ด๊ธฐ ๊ฐ์ ๋ฌธ์์ด"Intl.RelativeTimeFormat"
์ ๋๋ค. ์ด ์์ฑ์Object.prototype.toString()
์์ ์ฌ์ฉ๋ฉ๋๋ค.
์ธ์คํด์ค ๋ฉ์๋
Intl.RelativeTimeFormat.prototype.format()
-
์ฃผ์ด์ง
Intl.RelativeTimeFormat
๊ฐ์ฒด์ ๋ก์ผ์ผ๊ณผ ํ์ ์ต์ ์ ๋ฐ๋ผvalue
๊ณผunit
์ ํ์์ ๋ง์ถฅ๋๋ค. Intl.RelativeTimeFormat.prototype.formatToParts()
-
์ฌ์ฉ์ ์ง์ ๋ก์ผ์ผ ํ์์ ์ฌ์ฉํ ์ ์๋ ์๋ ์๊ฐ์ ํ์์ ๋ถ๋ถ์ ์ผ๋ก ํํํ
Array
๊ฐ์ฒด๋ฅผ ๋ฐํํฉ๋๋ค. Intl.RelativeTimeFormat.prototype.resolvedOptions()
-
๊ฐ์ฒด๋ฅผ ์ด๊ธฐํํ๋ ๋์ ๊ณ์ฐ๋ ๋ก์ผ์ผ ๋ฐ ํ์ ์ต์ ์ ๋ฐ์ํ๋ ์์ฑ์ ๊ฐ์ง ์ ๊ฐ์ฒด๋ฅผ ๋ฐํํฉ๋๋ค.
์์
๊ธฐ๋ณธ ํฌ๋งท ์ฌ์ฉํ๊ธฐ
๋ค์ ์์ ๋ ์์ด๋ฅผ ์ฌ์ฉํ ์๋ ์๊ฐ ํ์์ ๋ณํํ๋ ๊ฐ์ฒด๋ฅผ ๋ณด์ฌ์ค๋๋ค.
// ์๋ ์๊ฐ ํ์์ ๋ณํํ๋ ๊ฐ์ฒด๋ฅผ ๋ช
์์ ์ธ ๊ธฐ๋ณธ ๊ฐ๊ณผ ํจ๊ป ๋ง๋ญ๋๋ค.
const rtf = new Intl.RelativeTimeFormat("en", {
localeMatcher: "best fit", // ๋ ๋ค๋ฅธ ๊ฐ: "lookup"
numeric: "always", // ๋ ๋ค๋ฅธ ๊ฐ: "auto"
style: "long", // ๋ ๋ค๋ฅธ ๊ฐ: "short" or "narrow"
});
// ์์ ๊ฐ(-1)์ ์ฌ์ฉํ ์๋ ์๊ฐ ํ์
rtf.format(-1, "day"); // "1 day ago"
// ์์ ๊ฐ(1)์ ์ฌ์ฉํ ์๋ ์๊ฐ ํ์
rtf.format(1, "day"); // "in 1 day"
formatToParts ์ฌ์ฉํ๊ธฐ
๋ค์ ์์ ๋ ์์ ํ์์ผ๋ก ๋ฐํํ๋ ์๋ ์๊ฐ ํ์์ ๋ณํํ๋ ๊ฐ์ฒด๋ฅผ ๋ณด์ฌ์ค๋๋ค.
const rtf = new Intl.RelativeTimeFormat("en", { numeric: "auto" });
// day ๋จ์๋ฅผ ์ฌ์ฉํ ์๋ ์๊ฐ ํ์ ๋ณํ
rtf.formatToParts(-1, "day");
// [{ type: "literal", value: "yesterday"}]
rtf.formatToParts(100, "day");
// [
// { type: "literal", value: "in " },
// { type: "integer", value: "100", unit: "day" },
// { type: "literal", value: " days" }
// ]
๋ช ์ธ์
Specification |
---|
ECMAScriptยฎ 2026 Internationalization API Specification # relativetimeformat-objects |
๋ธ๋ผ์ฐ์ ํธํ์ฑ
Loadingโฆ