طرق الحصول على التاريخ في JavaScript
يمكن استخدام هذه الطرق للحصول على معلومات من كائن تاريخ:
Method | Description |
---|---|
getFullYear() | Get the year as a four digit number (yyyy) |
getMonth() | Get the month as a number (0-11) |
getDate() | Get the day as a number (1-31) |
getHours() | Get the hour (0-23) |
getMinutes() | Get the minute (0-59) |
getSeconds() | Get the second (0-59) |
getMilliseconds() | Get the millisecond (0-999) |
getTime() | Get the time (milliseconds since January 1, 1970) |
getDay() | Get the weekday as a number (0-6) |
Date.now() | Get the time. ECMAScript 5. |
طريقة getTime ()
تُرجع getTime()
الطريقة عدد المللي ثانية منذ 1 يناير 1970:
مثال
const d = new Date();
d.getTime();
طريقة getFullYear ()
تُرجع getFullYear()
الطريقة سنة التاريخ كرقم مكون من أربعة أرقام:
مثال
const d = new Date();
d.getFullYear();
طريقة getMonth ()
تُرجع getMonth()
الطريقة شهر التاريخ كرقم (0-11):
مثال
const d = new Date();
d.getMonth();
في JavaScript ، الشهر الأول (يناير) هو الشهر رقم 0 ، لذلك يُرجع شهر ديسمبر رقم الشهر 11.
يمكنك استخدام مصفوفة أسماء ، getMonth()
وإرجاع الشهر كاسم:
مثال
const months = ["January", "February", "March", "April", "May",
"June", "July", "August", "September", "October",
"November", "December"];
const d = new Date();
let month = months[d.getMonth()];
طريقة getDate ()
تُرجع getDate()
الطريقة يوم التاريخ كرقم (1-31):
مثال
const d = new Date();
d.getDate();
طريقة getHours ()
تُرجع getHours()
الطريقة ساعات التاريخ كرقم (0-23):
مثال
const d = new Date();
d.getHours();
طريقة getMinutes ()
تُرجع getMinutes()
الطريقة دقائق التاريخ كرقم (0-59):
مثال
const d = new Date();
d.getMinutes();
طريقة getSeconds ()
تُرجع getSeconds()
الطريقة الثواني من التاريخ كرقم (0-59):
مثال
const d = new Date();
d.getSeconds();
طريقة getMilliseconds ()
تقوم getMilliseconds()
الطريقة بإرجاع المللي ثانية من التاريخ كرقم (0-999):
مثال
const d = new Date();
d.getMilliseconds();
طريقة getDay ()
تُرجع getDay()
الطريقة يوم من أيام الأسبوع لتاريخ كرقم (0-6):
مثال
const d = new Date();
d.getDay();
في JavaScript ، يعني اليوم الأول من الأسبوع (0) "الأحد" ، حتى إذا اعتبرت بعض البلدان في العالم أن اليوم الأول من الأسبوع هو "الاثنين"
يمكنك استخدام مصفوفة من الأسماء ، getDay()
وإرجاع يوم الأسبوع كاسم:
مثال
const days = ["Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"];
const d = new Date();
let day = days[d.getDay()];
طرق التوقيت العالمي المنسق (UTC)
تُستخدم طرق تاريخ UTC للعمل مع تواريخ UTC (تواريخ المنطقة الزمنية العالمية):
Method | Description |
---|---|
getUTCDate() | Same as getDate(), but returns the UTC date |
getUTCDay() | Same as getDay(), but returns the UTC day |
getUTCFullYear() | Same as getFullYear(), but returns the UTC year |
getUTCHours() | Same as getHours(), but returns the UTC hour |
getUTCMilliseconds() | Same as getMilliseconds(), but returns the UTC milliseconds |
getUTCMinutes() | Same as getMinutes(), but returns the UTC minutes |
getUTCMonth() | Same as getMonth(), but returns the UTC month |
getUTCSeconds() | Same as getSeconds(), but returns the UTC seconds |
استكمال مرجع تاريخ جافا سكريبت
للحصول على مرجع كامل للتاريخ ، انتقل إلى:
استكمال مرجع تاريخ جافا سكريبت .
يحتوي المرجع على أوصاف وأمثلة لجميع خصائص وطرق التاريخ.