This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

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()

๊ฐ์ฒด๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๋™์•ˆ ๊ณ„์‚ฐ๋œ ๋กœ์ผ€์ผ ๋ฐ ํ˜•์‹ ์˜ต์…˜์„ ๋ฐ˜์˜ํ•˜๋Š” ์†์„ฑ์„ ๊ฐ€์ง„ ์ƒˆ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ์ œ

๊ธฐ๋ณธ ํฌ๋งท ์‚ฌ์šฉํ•˜๊ธฐ

๋‹ค์Œ ์˜ˆ์ œ๋Š” ์˜์–ด๋ฅผ ์‚ฌ์šฉํ•œ ์ƒ๋Œ€ ์‹œ๊ฐ„ ํ˜•์‹์„ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

js
// ์ƒ๋Œ€ ์‹œ๊ฐ„ ํ˜•์‹์„ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ๋ช…์‹œ์ ์ธ ๊ธฐ๋ณธ ๊ฐ’๊ณผ ํ•จ๊ป˜ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
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 ์‚ฌ์šฉํ•˜๊ธฐ

๋‹ค์Œ ์˜ˆ์ œ๋Š” ์š”์†Œ ํ˜•์‹์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ์ƒ๋Œ€ ์‹œ๊ฐ„ ํ˜•์‹์„ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

js
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

๋ธŒ๋ผ์šฐ์ € ํ˜ธํ™˜์„ฑ

๊ฐ™์ด ๋ณด๊ธฐ