วิธีการวันที่ JavaScript


สารบัญ

    แสดงสารบัญ


ตัวสร้าง วันที่ใหม่()

ใน 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>

วันที่รับวิธีการ

getFullYear()

รับ ปี เป็นตัวเลขสี่หลัก (yyyy)

getMonth()

รับ เดือน เป็นตัวเลข (0-11)

getDate()

รับวันเป็นตัวเลข (1-31)

getDay()

รับ วันทำงาน เป็นตัวเลข (0-6)

getHours()

รับ ชั่วโมง (0-23)

getMinutes()

รับ นาที (0-59)

getSeconds()

รับวินาที (0-59)

getMilliseconds()

รับ มิลลิวินาที (0-999)

getTime()

รับเวลา (มิลลิวินาทีนับตั้งแต่ 1 มกราคม 1970)

หมายเหตุ 1

วิธีการรับข้างต้นส่งคืน เวลาท้องถิ่น

เวลาสากล (UTC) ได้รับการบันทึกไว้ที่ด้านล่างของหน้านี้

โน้ต 2

วิธีการรับส่งข้อมูลกลับจากวัตถุวันที่ที่มีอยู่

ในวัตถุวันที่ เวลาเป็นแบบคงที่ "นาฬิกา" ไม่ใช่ "กำลังทำงาน"

เวลาในวัตถุวันที่ไม่เหมือนกับเวลาปัจจุบัน


วิธีการ 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

getUTCDate() / getDate()

ส่งกลับวันที่ UTC

getUTCFullYear() / getFullYear()

ส่งกลับปี UTC

getUTCMonth() / getMonth()

ส่งกลับเดือน UTC

getUTCDay() / getDay()

ส่งกลับวัน UTC

getUTCHours() / getHours()

ส่งกลับชั่วโมง UTC

getUTCMinutes() / getMinutes()

ส่งกลับนาที UTC

getUTCSeconds() / getSeconds()

ส่งกลับค่าวินาที UTC

getUTCMilliseconds() / getMilliseconds()

ส่งกลับค่ามิลลิวินาที 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 ให้สมบูรณ์

หากต้องการข้อมูลอ้างอิงวันที่โดยสมบูรณ์ โปรดไปที่:

อ้างอิงวันที่ JavaScript ให้สมบูรณ์

ข้อมูลอ้างอิงประกอบด้วยคำอธิบายและตัวอย่างของคุณสมบัติ Date ทั้งหมดและ วิธีการ