Date بتخزّن لحظة زمنية بالمللي ثانية من 1970، والعرض للعربي بيتعمل بـIntl.DateTimeFormat بدل تكوين النص بإيدك عشان اللغة والتوقيت يظبطوا.
How to format dates in JavaScript with Intl?
المثال
Date بتخزّن رقم واحد: ملّي ثانية من 1 يناير 1970 بتوقيت UTC. أي تنسيق بعد كده بيعتمد على المنطقة الزمنية. أشهر فخ: الشهور بتبدأ من الصفر.
const d = new Date(Date.UTC(2026, 11, 1)); // 11 يعني ديسمبر
d.toISOString(); // '2026-12-01T00:00:00.000Z'
d.getTime(); // عدد الملّي ثانية
new Date('2026-12-01'); // نص ISO بس: صيغة آمنة
Date.now(); // دلوقتيIntl
Intl بتنسّق الأرقام والتواريخ والعملات واللغات على حسب لغة المستخدم، من غير مكتبات. وبتفهم العربي كامل.
const d = new Date(Date.UTC(2026, 11, 1));
new Intl.DateTimeFormat('ar-EG', { dateStyle: 'full', timeZone: 'Africa/Cairo' }).format(d);
// 'الثلاثاء، ١ ديسمبر ٢٠٢٦'
new Intl.NumberFormat('ar-EG', { style: 'currency', currency: 'EGP' }).format(1234.5);
// '١٬٢٣٤٫٥٠ ج.م.'
new Intl.PluralRules('ar').select(3); // 'few'
new Intl.RelativeTimeFormat('ar', { numeric: 'auto' }).format(-1, 'day'); // 'أمس'الحساب
const start = new Date('2026-12-01');
const end = new Date('2026-12-25');
const days = (end - start) / 86_400_000; // 24التوقيت الصيفي بيخلّي اليوم أحيانًا 23 أو 25 ساعة. متفترضش إن اليوم 86400 ثانية في حساب التقويم. وتخزين التواريخ يكون دايمًا بصيغة ISO بتوقيت UTC.
الحالة اللي بتعضّ
الشهور في Date بتبدأ من 0، والقيم الزيادة بتلفّ للشهر اللي بعده من غير error. new Date(2026, 1, 30) مش Invalid Date: هي 2 مارس.
console.log(new Date('2026-13-45')); // Invalid Date
console.log(new Date(2026, 1, 30)); // 2 مارس! الشهور بتلفّ
console.log(new Date(2026, 0, 1).getMonth()); // 0 — يناير بصفرالبرهان
const d = new Date(Date.UTC(2026, 11, 1));
console.log(d.toISOString(), new Date(2026, 0, 1).getMonth());
console.log(new Intl.DateTimeFormat('ar-EG', { dateStyle: 'full', timeZone: 'Africa/Cairo' }).format(d));
console.log(new Intl.PluralRules('ar').select(3), new Intl.RelativeTimeFormat('ar', { numeric: 'auto' }).format(-1, 'day'));2026-12-01T00:00:00.000Z 0 الثلاثاء، ١ ديسمبر ٢٠٢٦ few أمس
const d = new Date(Date.UTC(2026, 11, 1));
console.log(d.toISOString(), new Date(2026, 0, 1).getMonth());
console.log(new Intl.DateTimeFormat('ar-EG', { dateStyle: 'full', timeZone: 'Africa/Cairo' }).format(d));
console.log(new Intl.PluralRules('ar').select(3), new Intl.RelativeTimeFormat('ar', { numeric: 'auto' }).format(-1, 'day'));التمرين
اكتب function daysUntil(isoDate) بترجّع عدد الأيام المتبقية بالعربي: "بكرة" أو "بعد 3 أيام".
جرّب بنفسك قبل ما تروح للدرس اللي بعده.
تدريبات متدرّجة
كام يوم بينهم
اكتب daysBetween(a, b) بترجّع عدد الأيام بين تاريخين كرقم صحيح.
function daysBetween(a, b) {
}
تنسيق الأرقام
اكتب formatNum(n) بترجّع الرقم منسّق بالفواصل الإنجليزية، زي 1,234.5.
function formatNum(n) {
}
زوّد أيام من غير ما تغيّر
اكتب addDays(date, n) بترجّع تاريخ جديد بعد n يوم، والتاريخ الأصلي ميتغيّرش.
function addDays(date, n) {
}
تحدّي
اتجرّب على Node v26.14.0 · آخر تشغيل