วันที่ใหม่()
ใน JavaScript วัตถุวันที่จะถูกสร้างขึ้นด้วย new Date()
new Date()
ส่งคืนวัตถุวันที่ที่มีวันที่และเวลาปัจจุบัน
const date = new Date();
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>Using new Date()</h2>
<p>Create a new date object with the current date and time:</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
รับ ปี เป็นตัวเลขสี่หลัก (yyyy)
รับ เดือน เป็นตัวเลข (0-11)
รับวันเป็นตัวเลข (1-31)
รับ วันทำงาน เป็นตัวเลข (0-6)
รับ ชั่วโมง (0-23)
รับ นาที (0-59)
รับวินาที (0-59)
รับ มิลลิวินาที (0-999)
รับเวลา (มิลลิวินาทีนับตั้งแต่ 1 มกราคม 1970)
วิธีการรับข้างต้นส่งคืน เวลาท้องถิ่น
เวลาสากล (UTC) ได้รับการบันทึกไว้ที่ด้านล่างของหน้านี้
วิธีการรับส่งข้อมูลกลับจากวัตถุวันที่ที่มีอยู่
ในวัตถุวันที่ เวลาเป็นแบบคงที่ "นาฬิกา" ไม่ใช่ "กำลังทำงาน"
เวลาในวัตถุวันที่ไม่เหมือนกับเวลาปัจจุบัน
getFullYear()
getFullYear()
วิธีการส่งกลับปีของวันที่เป็นตัวเลขสี่หลัก:
const d = new Date("2021-03-25");
d.getFullYear();
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getFullYear() Method</h2>
<p>Return the full year of a date object:</p>
<p id="demo"></p>
<script>
const d = new Date("2021-03-25")
document.getElementById("demo").innerHTML = d.getFullYear();
</script>
</body>
</html>
const d = new Date();
d.getFullYear();
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getFullYear() Method</h2>
<p>Return the full year of a date object:</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getFullYear();
</script>
</body>
</html>
โค้ด JavaScript เก่าอาจใช้เมธอด getYear() ที่ไม่เป็นไปตามมาตรฐาน
getYear() ควรส่งคืนปีที่มีตัวเลข 2 หลัก
getYear() เลิกใช้แล้ว อย่าใช้มัน!
getMonth()
getMonth()
วิธีการส่งกลับเดือนของวันที่เป็นตัวเลข (0-11)
ใน JavaScript มกราคมคือเดือนหมายเลข 0 กุมภาพันธ์คือหมายเลข 1 ...
ในที่สุดเดือนธันวาคมก็เป็นเดือนที่ 11
const d = new Date("2021-03-25");
d.getMonth();
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getMonth() Method</h2>
<p>Return the month of a date as a number from 0 to 11.</p>
<p>To get the correct month number, you must add 1:</p>
<p id="demo"></p>
<script>
const d = new Date("2021-03-25");
document.getElementById("demo").innerHTML = d.getMonth() + 1;
</script>
</body>
</html>
const d = new Date();
d.getMonth();
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getMonth() Method</h2>
<p>Return the month of a date as a number from 0 to 11.</p>
<p>To get the correct month number, you must add 1:</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getMonth() + 1;
</script>
</body>
</html>
คุณสามารถใช้อาร์เรย์ของชื่อเพื่อส่งคืนเดือนเป็นชื่อได้:
const months = ["January", "February", "March", "April", "May",
"June", "July", "August", "September", "October",
"November", "December"];
const d = new Date("2021-03-25");
let month = months[d.getMonth()];
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>JavaScript getMonth()</h2>
<p>Return the month as a number.</p>
<p>You can use an array of names to return the month as a name:</p>
<p id="demo"></p>
<script>
const months = ["January","February","March","April","May","June","July","August","September","October","November","December"];
const d = new Date("2021-03-25");
let month = months[d.getMonth()];
document.getElementById("demo").innerHTML = month;
</script>
</body>
</html>
const months = ["January", "February", "March", "April", "May",
"June", "July", "August", "September", "October",
"November", "December"];
const d = new Date();
let month = months[d.getMonth()];
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>JavaScript getMonth()</h2>
<p>Return the month as a number.</p>
<p>You can use an array of names to return the month as a name:</p>
<p id="demo"></p>
<script>
const months = ["January","February","March","April","May","June","July","August","September","October","November","December"];
const d = new Date();
let month = months[d.getMonth()];
document.getElementById("demo").innerHTML = month;
</script>
</body>
</html>
getDate()
getDate()
วิธีการส่งกลับวันของวันที่เป็นตัวเลข (1-31):
const d = new Date("2021-03-25");
d.getDate();
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getDate() Method</h2>
<p>Return the day of a date as a number (1-31):</p>
<p id="demo"></p>
<script>
const d = new Date("2021-03-25");
document.getElementById("demo").innerHTML = d.getDate();
</script>
</body>
</html>
const d = new Date();
d.getDate();
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getDate() Method</h2>
<p>Return the day of a date as a number (1-31):</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getDate();
</script>
</body>
</html>
getHours()
getHours()
วิธีการส่งกลับชั่วโมงของวันที่เป็นตัวเลข (0-23):
const d = new Date("2021-03-25");
d.getHours();
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getHours() Method</h2>
<p>Return the hours of a date as a number (0-23):</p>
<p id="demo"></p>
<script>
const d = new Date("2021-03-25");document.getElementById("demo").innerHTML = d.getHours();
</script>
</body>
</html>
const d = new Date();
d.getHours();
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getHours() Method</h2>
<p>Return the hours of a date as a number (0-23):</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getHours();
</script>
</body>
</html>
getMinutes()
getMinutes()
วิธีการส่งกลับนาทีของวันที่เป็นตัวเลข (0-59):
const d = new Date("2021-03-25");
d.getMinutes();
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getMinutes() Method</h2>
<p>Returns the minutes of a date as a number (0-59):</p>
<p id="demo"></p>
<script>
const d = new Date("2021-03-25");
document.getElementById("demo").innerHTML = d.getMinutes();
</script>
</body>
</html>
const d = new Date();
d.getMinutes();
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getMinutes() Method</h2>
<p>Returns the minutes of a date as a number (0-59):</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getMinutes();
</script>
</body>
</html>
getSeconds()
getSeconds()
วิธีการส่งกลับวินาทีของวันที่เป็นตัวเลข (0-59):
const d = new Date("2021-03-25");
d.getSeconds();
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getSeconds() Method</h2>
<p>Return the seconds of a date as a number (0-59):</p>
<p id="demo"></p>
<script>
const d = new Date("2021-03-25");
document.getElementById("demo").innerHTML = d.getSeconds();
</script>
</body>
</html>
const d = new Date();
d.getSeconds();
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getSeconds() Method</h2>
<p>Return the seconds of a date as a number (0-59):</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getSeconds();
</script>
</body>
</html>
getMilliseconds()
getMilliseconds()
วิธีการส่งกลับมิลลิวินาทีของวันที่เป็นตัวเลข (0-999):
const d = new Date("2021-03-25");
d.getMilliseconds();
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getMilliseconds() Method</h2>
<p>Return the milliseconds of a date as a number (0-999):</p>
<p id="demo"></p>
<script>
const d = new Date("2021-03-25");
document.getElementById("demo").innerHTML = d.getMilliseconds();
</script>
</body>
</html>
const d = new Date();
d.getMilliseconds();
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getMilliseconds() Method</h2>
<p>Return the milliseconds of a date as a number (0-999):</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getMilliseconds();
</script>
</body>
</html>
getDay()
getDay()
วิธีการส่งกลับวันในสัปดาห์ของวันที่เป็นตัวเลข (0-6)
ใน JavaScript วันแรกของสัปดาห์ (วันที่ 0) คือวันอาทิตย์
บางประเทศในโลกถือว่าวันแรกของสัปดาห์เป็นวันจันทร์
const d = new Date("2021-03-25");
d.getDay();
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getDay() Method</h2>
<p>Return the weekday as a number:</p>
<p id="demo"></p>
<script>
const d = new Date("2021-03-25");
document.getElementById("demo").innerHTML = d.getDay();
</script>
</body>
</html>
const d = new Date();
d.getDay();
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getDay() Method</h2>
<p>Return the weekday as a number:</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getDay();
</script>
</body>
</html>
คุณสามารถใช้อาร์เรย์ของชื่อและ getDay()
เพื่อส่งคืนวันทำงานเป็นชื่อ:
const days = ["Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"];
const d = new Date("2021-03-25");
let day = days[d.getDay()];
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getDay() Method</h2>
<p>Return the weekday as a number.</p>
<p>You can use an array of names to return the weekday as a name:</p>
<p id="demo"></p>
<script>
const days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
const d = new Date("2021-03-25");
let day = days[d.getDay()];
document.getElementById("demo").innerHTML = day;
</script>
</body>
</html>
const days = ["Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"];
const d = new Date();
let day = days[d.getDay()];
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getDay() Method</h2>
<p>Return the weekday as a number.</p>
<p>You can use an array of names to return the weekday as a name:</p>
<p id="demo"></p>
<script>
const days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
const d = new Date();
let day = days[d.getDay()];
document.getElementById("demo").innerHTML = day;
</script>
</body>
</html>
getTime()
getTime()
วิธีการส่งกลับจำนวนมิลลิวินาทีตั้งแต่วันที่ 1 มกราคม 1970:
const d = new Date("1970-01-01");
d.getTime();
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getTime() Method</h2>
<p></p>
<p>Return the number of milliseconds since midnight January 1, 1970:</p>
<p id="demo"></p>
<script>
const d = new Date("1970-01-01");
document.getElementById("demo").innerHTML = d.getTime();
</script>
</body>
</html>
const d = new Date("2021-03-25");
d.getTime();
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getTime() Method</h2>
<p></p>
<p>Return the number of milliseconds since midnight January 1, 1970:</p>
<p id="demo"></p>
<script>
const d = new Date("2021-03-25");
document.getElementById("demo").innerHTML = d.getTime();
</script>
</body>
</html>
const d = new Date();
d.getTime();
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getTime() Method</h2>
<p></p>
<p>Return the number of milliseconds since midnight January 1, 1970:</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getTime();
</script>
</body>
</html>
Date.now()
Date.now()
ส่งคืนจำนวนมิลลิวินาทีนับตั้งแต่ 1 มกราคม 1970
let ms = Date.now();
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The Date.now() Method</h2>
<p>Return the current date/time in milliseconds since January 1, 1970:</p>
<p id="demo"></p>
<script>
const date = Date.now();
document.getElementById("demo").innerHTML = date;
</script>
</body>
</html>
คำนวณจำนวนปีตั้งแต่ปี 1970/01/01:
const minute = 1000 * 60;
const hour = minute * 60;
const day = hour * 24;
const year = day * 365;
let years = Math.round(Date.now() / year);
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>Using Date.now()</h2>
<p>Calculate the number of years since January 1, 1970:</p>
<p id="demo"></p>
<script>
// Calculate milliseconds in a year
const minute = 1000 * 60;
const hour = minute * 60;
const day = hour * 24;
const year = day * 365;
// Divide Date.now() with a year
let years = Math.round(Date.now() / year);
document.getElementById("demo").innerHTML = years;
</script>
</body>
</html>
Date.now()
เป็นวิธีการคงที่ของวัตถุ Date
คุณไม่สามารถใช้มันบนวัตถุวันที่เช่น myDate.now()
ไวยากรณ์จะเป็น Date.now()
เสมอ
ส่งกลับวันที่ UTC
ส่งกลับปี UTC
ส่งกลับเดือน UTC
ส่งกลับวัน UTC
ส่งกลับชั่วโมง UTC
ส่งกลับนาที UTC
ส่งกลับค่าวินาที UTC
ส่งกลับค่ามิลลิวินาที UTC
UTC methods use UTC time (Coordinated Universal Time).
UTC time is the same as GMT (Greenwich Mean Time).
The difference between Local time and UTC time can be up to 24 hours.
getTimezoneOffset()
วิธีการ getTimezoneOffset()
ส่งกลับค่าความแตกต่าง (เป็นนาที) ระหว่างเวลาท้องถิ่นกับเวลา UTC:
let diff = d.getTimezoneOffset();
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getTimezoneOffset() Method</h2>
<p>The time zone difference in minutes is:</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getTimezoneOffset();
</script>
</body>
</html>
หากต้องการข้อมูลอ้างอิงวันที่โดยสมบูรณ์ โปรดไปที่:
อ้างอิงวันที่ JavaScript ให้สมบูรณ์
ข้อมูลอ้างอิงประกอบด้วยคำอธิบายและตัวอย่างของคุณสมบัติ Date ทั้งหมดและ วิธีการ