٤.٧ أنواع البيانات المركّبة — الجزء الثاني¶
Date()¶
التمرين 1: تأمّل الشيفرة التالية.
var dateNow = new Date();
console.log(`The date: ${dateNow}`);
النتيجة هي: The date: Sat Sep 05 2020 11:51:49 GMT+0300 (Eastern European Summer Time)
كائن Date
العامل Date يُنشئ كائنات تاريخ جديدة. عند استدعائه دون وسائط يخزّن التاريخ والوقت الحاليين.
لإنشاء كائن تاريخ باستخدام Date() ، استخدم الكلمة المفتاحية new متبوعةً بـ Date().
التمرين 2: ارجع إلى التمرين 1؛ ما الدالة البانية لـ dateNow.
var dateNow = new Date();
console.log(`The constructor is: ${dateNow.constructor}`);
النتيجة هي: The constructor is: function Date() { [native code] }
التمرين 3: ما قيمة valueOf لكائن التاريخ؟
var dateNow = new Date();
console.log(`dateNow.valueOf(): ${dateNow.valueOf()}`);
النتيجة هي: dateNow.valueOf(): 1599412703986
الوقت الحالي¶
التمرين 4: ما الوقت الحالي؟ تلميح: استخدم Date.now()
console.log(`The time now is: ${Date.now()}`);
النتيجة هي: The time now is: 1599413110712
التمرين 5: ما الوقت الحالي؟ تلميح: استخدم new Date().getTime().
var dateNow = new Date();
console.log(`dateNow.getTime(): ${dateNow.getTime()}`);
النتيجة هي: dateNow.getTime(): 1599413356554
التمرين 6: افحص نتيجة الشيفرة التالية.
console.log(new Date().valueOf(), Date.now(), new Date().getTime())
النتيجة هي: 1599413475310 1599413475310 1599413475310
معلومة
new Date.valueOf(), Date.now() و new Date().getTime() تُرجع عدد الملّي ثواني منذ بداية 1 يناير 1970 بتوقيت UTC.
جلب مكوّنات التاريخ¶
التمرين 7: ما رقم اليوم الحالي من الشهر؟ تلميح: استخدم new Date().getDate().
var dateNow = new Date();
var dayOfMonth = dateNow.getDate();
console.log(`The full date is: ${dateNow}`);
console.log(`The day number is: ${dayOfMonth}`);
النتيجة كما يلي:
The full date is: Sun Sep 06 2020 20:38:11 GMT+0300 (Eastern European Summer Time)
The day number is: 6
new Date().getDate()
new Date().getDate() تُرجع يوم الشهر من 1 إلى 31 وفق التوقيت المحلي.
التمرين 8: ما رقم اليوم الحالي من الأسبوع؟ تلميح: استخدم new Date().getDay().
var dateNow = new Date();
var dayOfWeek = dateNow.getDay();
console.log(`The full date is: ${dateNow}`);
console.log(`The day number is: ${dayOfWeek}`);
النتيجة كما يلي:
The full date is: Sun Sep 06 2020 20:41:56 GMT+0300 (Eastern European Summer Time)
The day number is: 0
new Date().getDay()
new Date().getDay() تُرجع يوم الأسبوع من 0 إلى 6 وفق التوقيت المحلي؛ الأحد هو 0.
ملاحظة: ترقيم JavaScript يبدأ بالأحد دائمًا. كان يوم تنفيذ المثال أحدًا، لذا new Date().getDay() تُرجع 0. قد تختلف النتيجة بحسب يوم التنفيذ.
التمرين 9: ما السنة الحالية كاملة؟ تلميح: استخدم new Date().getFullYear().
var dateNow = new Date();
var fullYear = dateNow.getFullYear();
console.log(`The full date is: ${dateNow}`);
console.log(`The 4 digit year is: ${fullYear}`);
النتيجة كما يلي:
The full date is: Sun Sep 06 2020 20:48:45 GMT+0300 (Eastern European Summer Time)
The 4 digit year is: 2020
new Date().getFullYear()
new Date().getFullYear() تُرجع رقم السنة كاملًا وفق التوقيت المحلي.
التمرين 10: جرّب الطريقة القديمة لجلب السنة. تلميح: استخدم new Date().getYear().
var dateNow = new Date();
var year = dateNow.getYear();
console.log(`The full date is: ${dateNow}`);
console.log(`The 2-3-digit year is: ${year}`);
النتيجة كما يلي:
The full date is: Sun Sep 06 2020 20:50:13 GMT+0300 (Eastern European Summer Time)
The 2-3-digit year is: 120
new Date().getYear()
new Date().getYear() تُرجع السنة ناقص 1900. هذه طريقة قديمة؛ يُفضّل استخدام getFullYear().
التمرين 11: ما يوم الشهر ويوم الأسبوع والسنة الحالية؟
var date = new Date();
console.log(`The current day of the month: ${date.getDate()}`);
console.log(`The current day of the week: ${date.getDay()}`);
console.log(`The current year: ${date.getFullYear()}`);
النتيجة كما يلي:
The current day of the month: 6
The current day of the week: 0
The current year: 2020
جلب مكوّنات الوقت¶
التمرين 12: ما الساعة الحالية؟ تلميح: استخدم new Date().getHours().
var hours = new Date().getHours();
console.log(`The current hour: ${hours}`);
النتيجة هي: The current hour: 21
new Date().getHours()
new Date().getHours() تُرجع الساعة من 0 إلى 23 وفق التوقيت المحلي.
التمرين 13: ما الدقيقة الحالية؟ تلميح: new Date().getMinutes().
var minutes = new Date().getMinutes();
console.log(`The current minute: ${minutes}`);
النتيجة هي: The current minute: 9
new Date().getMinutes()
new Date().getMinutes() تُرجع الدقائق من 0 إلى 59 وفق التوقيت المحلي.
التمرين 14: ما الثانية الحالية؟ تلميح: new Date().getSeconds().
var seconds = new Date().getSeconds();
console.log(`The current second: ${seconds}`);
النتيجة هي: The current second: 16
new Date().getSeconds()
new Date().getSeconds() تُرجع الثواني من 0 إلى 59 وفق التوقيت المحلي.
التمرين 15: ما الساعة والدقيقة والثانية الحالية؟
var date = new Date();
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
console.log(`The date: ${date}`);
console.log(`The current hour: ${hours}`);
console.log(`The current minute: ${minutes}`);
console.log(`The current second: ${seconds}`);
النتيجة كما يلي:
The date: Sun Sep 06 2020 21:22:25 GMT+0300 (Eastern European Summer Time)
The current hour: 21
The current minute: 22
The current second: 25
تعيين مكوّنات التاريخ¶
التمرين 16: أنشئ كائن تاريخ واضبط يوم الشهر على 5. تلميح: استخدم new Date().setDate(5).
var date = new Date();
date.setDate(5);
console.log(date.getDate());
النتيجة هي: 5
التمرين 17: ارجع إلى التمرين 16؛ اضبط فهرس الشهر على 10، أي نوفمبر. تلميح: استخدم new Date().setMonth(10).
var date = new Date();
date.setDate(5);
date.setMonth(10);
console.log(date.getMonth());
النتيجة هي: 10
التمرين 18: ارجع إلى التمرين 17؛ اضبط السنة على 1998. تلميح: استخدم new Date().setFullYear(1998)، أو new Date().setYear(98)
var date = new Date();
date.setDate(5);
date.setMonth(10);
date.setFullYear(1998);
console.log(date.getFullYear());
النتيجة هي: 1998
تعيين مكوّنات الوقت¶
التمرين 19: ارجع إلى التمرين 18؛ اضبط الساعة على 22. تلميح: استخدم new Date().setHours(22).
var date = new Date();
date.setDate(5);
date.setMonth(10);
date.setFullYear(1998);
date.setHours(22);
console.log(date.getHours());
النتيجة هي: 22
التمرين 20: ارجع إلى التمرين 19؛ اضبط الدقيقة على 50. تلميح: استخدم new Date().setMinutes(50).
var date = new Date();
date.setDate(5);
date.setMonth(10);
date.setFullYear(1998);
date.setHours(22);
date.setMinutes(50);
console.log(date.getMinutes());
النتيجة هي: 50
التمرين 21: ارجع إلى التمرين 20؛ اضبط الثانية على 11. تلميح: استخدم new Date().setSeconds(11).
var date = new Date();
date.setDate(5);
date.setMonth(10);
date.setFullYear(1998);
date.setHours(22);
date.setMinutes(50);
date.setSeconds(11);
console.log(date.getSeconds());
console.log(date);
النتيجة كما يلي:
11
Thu Nov 05 1998 22:50:11 GMT+0200 (Eastern European Standard Time)
تدريبات إضافية¶
التمرين 22: أنشئ تاريخًا لليوم 12 من الشهر ذي الفهرس 5، سنة 1980، الساعة 14:17:02. الفهرس 5 يعني يونيو.
var date = new Date();
date.setDate(12);
date.setMonth(5);
date.setYear(80);
date.setHours(14);
date.setMinutes(17);
date.setSeconds(2);
console.log(date);
النتيجة هي: Thu Jun 12 1980 14:17:02 GMT+0200 (Eastern European Standard Time)
التمرين 23: ارجع إلى التمرين 22؛ اعرض يوم الشهر والشهر والسنة والساعة والدقيقة والثانية.
var date = new Date();
date.setDate(12);
date.setMonth(5);
date.setYear(80);
date.setHours(14);
date.setMinutes(17);
date.setSeconds(2);
console.log(date.getDate());
console.log(date.getMonth());
console.log(date.getYear());
console.log(date.getHours());
console.log(date.getMinutes());
console.log(date.getSeconds());
النتيجة كما يلي:
12
5
80
14
17
2
التمرين 24: أنشئ تاريخًا لليوم 1 من الشهر ذي الفهرس 11، سنة 2758، واضبط الوقت كما في المثال التالي.
var date = new Date();
date.setDate(01);
date.setMonth(11);
date.setYear(2758);
date.setHours(01);
date.setMinutes(11);
date.setSeconds(00);
console.log(date);
النتيجة هي: Mon Dec 01 2758 01:11:00 GMT+0200 (Eastern European Standard Time)
التمرين 25: ارجع إلى التمرين 24؛ اعرض يوم الشهر والشهر والسنة والساعة والدقيقة والثانية في وحدة التحكم.
var date = new Date();
date.setDate(01);
date.setMonth(11);
date.setYear(2758);
date.setHours(01);
date.setMinutes(11);
date.setSeconds(00);
console.log(date.getDate());
console.log(date.getMonth());
console.log(date.getFullYear());
console.log(date.getHours());
console.log(date.getMinutes());
console.log(date.getSeconds());
النتيجة كما يلي:
1
11
2758
1
11
0