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 |
อ้างอิงวันที่ JavaScript ที่สมบูรณ์
สำหรับการอ้างอิงวันที่ฉบับสมบูรณ์ ไปที่:
อ้างอิงวันที่ของ JavaScriptให้สมบูรณ์
ข้อมูลอ้างอิงประกอบด้วยคำอธิบายและตัวอย่างของคุณสมบัติและเมธอดของ Date ทั้งหมด