Skip to content

Instantly share code, notes, and snippets.

@icesernia
Created March 8, 2021 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icesernia/eea0ea1291fc5b058354d9d797d955f7 to your computer and use it in GitHub Desktop.
Save icesernia/eea0ea1291fc5b058354d9d797d955f7 to your computer and use it in GitHub Desktop.
const rtf = new Intl.RelativeTimeFormat("th", {
localeMatcher: "best fit",
numeric: "always",
style: "long",
});
console.log(rtf.format(-1, "day")); // expected output: 1 วันที่ผ่านมา
console.log(rtf.format(-1, "month")); // expected output: 1 เดือนที่ผ่านมา
console.log(rtf.format(-1, "year")); // expected output: 1 ปีที่แล้ว
console.log(rtf.format(1, "day")); // expected output: ในอีก 1 วัน
console.log(rtf.format(0, "year")); // expected output: ในอีก 0 ปี
const rtf1 = new Intl.RelativeTimeFormat("th", { numeric: "auto" });
console.log(rtf1.format(-1, "day")); // expected output: เมื่อวาน
console.log(rtf1.format(-1, "month")); // expected output: เดือนที่แล้ว
console.log(rtf1.format(-1, "year")); // expected output: ปีที่แล้ว
console.log(rtf1.format(1, "day")); // expected output: พรุ่งนี้
console.log(rtf1.format(0, "year")); // expected output: ปีนี้
const rtf2 = new Intl.RelativeTimeFormat("en", { numeric: "auto" });
console.log(rtf2.format(-1, "day")); // expected output: yesterday
console.log(rtf2.format(-1, "month")); // expected output: last month
console.log(rtf2.format(-1, "year")); // expected output: last year
console.log(rtf2.format(1, "day")); // expected output: tomorrow
console.log(rtf2.format(0, "year")); // expected output: this year
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment