Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

ฟังก์ชัน Date.toDateString() ใน JavaScript

ฟังก์ชัน toDateString() ของออบเจกต์ Date คืนค่า ส่วนของวันที่ (ไม่รวมเวลา) ในรูปแบบสตริงที่อ่านง่าย

ไวยากรณ์

dateObj.toDateString();

ตัวอย่าง

<html> <head>    <title>JavaScript Example</title> </head> <body>    <script type="text/javascript">       var dateObj = new Date('September 26, 89 12:4:25:96');       document.write("วันที่ปัจจุบัน: "+dateObj.toDateString());    </script> </body> </html>

ผลลัพธ์

วันที่ปัจจุบัน: Tue Sep 26 1989

ตัวอย่าง: ใช้วันที่ปัจจุบัน

<html> <head>    <title>JavaScript Example</title> </head> <body>    <script type="text/javascript">       var dateObj = new Date();       document.write("วันที่ปัจจุบัน: "+dateObj.toDateString());    </script> </body> </html>

ผลลัพธ์

วันที่ปัจจุบัน: 18/10/2018

รูปแบบผลลัพธ์:

Day Mon DD YYYY (เช่น Thu Oct 18 2018)

เปรียบเทียบเมธอดวันที่:

เมธอดผลลัพธ์ตัวอย่างรวมเวลา
toDateString()Thu Oct 18 2018ไม่
toTimeString()14:30:25 GMT+0700เฉพาะเวลา
toString()Thu Oct 18 2018 14:30:25 GMT+0700ใช่
toLocaleDateString()18/10/2024ไม่ (locale)
toISOString()2024-10-18T07:30:25.000Zใช่ (UTC)