بومی سازی تاریخ و اعداد در جاوا اسکریپت در سال 2020
نویسنده: وحید نصیری
تاریخ: ۱۳۹۸/۱۲/۰۱ ۱۳:۵۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
function formatDate(d)
{
var month = d.getMonth() + 1;
var date = d.getDate();
var year = d.getFullYear();
return month + "/" + date + "/" + year;
}
function formatMoney(amount)
{
return "$" + amount.toFixed(2);
} const result = new Intl.NumberFormat("fa").format(123456) const portugueseTime = new Intl.DateTimeFormat(["pt-BR", "pt-PT"], options);
{
weekday: 'narrow' | 'short' | 'long',
era: 'narrow' | 'short' | 'long',
year: 'numeric' | '2-digit',
month: 'numeric' | '2-digit' | 'narrow' | 'short' | 'long',
day: 'numeric' | '2-digit',
hour: 'numeric' | '2-digit',
minute: 'numeric' | '2-digit',
second: 'numeric' | '2-digit',
timeZoneName: 'short' | 'long',
// Time zone to express it in
timeZone: 'Asia/Shanghai',
// Force 12-hour or 24-hour
hour12: true | false,
// Rarely-used options
hourCycle: 'h11' | 'h12' | 'h23' | 'h24',
formatMatcher: 'basic' | 'best fit'
} var dateFormat = new Intl.DateTimeFormat("fa");
console.log(dateFormat.format(Date.now())); const date = new Date(Date.UTC(2020, 1, 20, 3, 0, 0, 200));
const faDate = new Intl.DateTimeFormat("fa", {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric"
}).format(date);
console.log(faDate); const isoString = new Date().toISOString();
const date = new Date(isoString);
console.log(
new Intl.DateTimeFormat("fa", {
month: "long",
day: "numeric",
year: "numeric"
}).format(date)
); console.log(
new Date().toLocaleDateString("fa", {
month: "long",
day: "numeric",
year: "numeric"
})
); const date = new Date(Date.UTC(2020, 1, 20, 3, 0, 0, 200));
const fmt = new Intl.DateTimeFormat("fa", {
year: "2-digit",
month: "numeric",
day: "numeric",
hour: "numeric",
minute: "numeric"
});
console.log(fmt.format(date)); const faDateTime = new Intl.DateTimeFormat("fa", {
year: "2-digit",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
timeZoneName: "short"
}).format;
const now = Date.now();
console.log(faDateTime(now)); const faTime = new Intl.DateTimeFormat("fa", {
year: "numeric",
month: "long",
day: "numeric",
hour: "2-digit",
minute: "2-digit",
timeZoneName: "short",
timeZone: "UTC"
});
console.log(faTime.format(now)); const rtf = new Intl.RelativeTimeFormat("en", {
localeMatcher: "best fit", // other values: "lookup"
numeric: "always", // other values: "auto"
style: "long", // other values: "short" or "narrow"
});
console.log(rtf.format(-1, "day"));
console.log(rtf.format(1, "day")); console.log(new Intl.NumberFormat("fa").format(123456)); console.log(
new Intl.NumberFormat("fa", { useGrouping: false }).format(123456)
); console.log(new Intl.NumberFormat("fa").format("تست")); const gasPrice = new Intl.NumberFormat("fa", {
style: "currency",
currency: "IRR",
minimumFractionDigits: 3
});
console.log(gasPrice.format(5.2597)); const faPercent = new Intl.NumberFormat("fa", {
style: "percent",
minimumFractionDigits: 2
}).format;
console.log(faPercent(0.438)); const persianDecimal = new Intl.NumberFormat("fa", {
minimumIntegerDigits: 2,
maximumFractionDigits: 2
});
console.log(persianDecimal.format(3.1416)); function timeDiff(current, prev) {
const millisecond = current - prev;
const second = millisecond / 1000;
const minute = second / 60;
const hour = minute / 60;
const day = hour / 24;
const week = day / 7;
const month = week / 4.3;
const year = month / 12;
const quarter = year / 4;
const unitValues = {
millisecond,
second,
minute,
hour,
day,
week,
month,
year,
quarter
};
return function diffValueByUnit(unitKey) {
return unitValues[unitKey];
};
}
const from = new Date();
from.setDate(from.getDate() - 2);
console.log(from);
const to = new Date(Date.now());
console.log(to);
const diff = timeDiff(from, to);
const result = parseFloat(Math.round(diff("hour")));
console.log(result);
const rtf = new Intl.RelativeTimeFormat("fa");
console.log(rtf.format(result, "hour")); > Wed Feb 19 2020 02:24:36 GMT+0330 (Iran Standard Time) > Fri Feb 21 2020 02:24:36 GMT+0330 (Iran Standard Time) > -48 > "۴۸ ساعت پیش"
const date = new Date(Date.UTC(2020, 1, 20, 3, 0, 0, 200));
const faDate = new Intl.DateTimeFormat("fa", {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric"
}).format(date);
console.log(faDate); const date = new Date(Date.UTC(2020, 1, 20, 3, 0, 0, 200));
const faDateParts = new Intl.DateTimeFormat("fa", {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric"
}).formatToParts(date);
console.log(faDateParts); [
{ type: "year", value: "۱۳۹۸" },
{ type: "literal", value: " " },
{ type: "month", value: "اسفند" },
{ type: "literal", value: " " },
{ type: "day", value: "۱" },
{ type: "literal", value: ", " },
{ type: "weekday", value: "پنجشنبه" }
] const { year, literal, month, day, weekday } = Object.fromEntries(
new Intl.DateTimeFormat("fa", {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric"
})
.formatToParts(date)
.map(item => [item.type, item.value])
); const faDate = `${weekday}${literal}${day} ${month} ${year}`;
console.log(faDate); const faWithLatinNumbersFormatter = new Intl.DateTimeFormat('fa-IR-u-ca-persian-nu-latn');
const today = faWithLatinNumbersFormatter.format(Date.now());
console.log(today); // = 1399/1/10 nu's possible values can be: arab, arabext, bali, beng, deva, fullwide, gujr, guru, hanidec, khmr, knda, laoo, latn, limb, mlym, mong, mymr, orya, tamldec, telu, thai, tibt. ca's possible values include: buddhist, chinese, coptic, ethiopia, ethiopic, gregory, hebrew, indian, islamic, iso8601, japanese, persian, roc.