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

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

ฟังก์ชัน toString() ของออบเจกต์ Date คืนค่า การแสดงผลสตริงของวันที่ แบบเต็มรูปแบบ

ไวยากรณ์

dateObj.toString()

ตัวอย่าง

<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.toString());    </script> </body> </html>

ผลลัพธ์

วันที่ปัจจุบัน: Tue Sep 26 1989 12:04:25 GMT+0530 (India Standard Time)

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

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

ผลลัพธ์

วันที่ปัจจุบัน: Thu Oct 18 2018 15:20:42 GMT+0530 (India Standard Time)

รูปแบบผลลัพธ์มาตรฐาน:

Day Mon DD YYYY HH:MM:SS GMT±HHMM (Timezone)

การใช้งานทั่วไป:

  • ใช้สำหรับ debug และ logging
  • การแปลงอัตโนมัติเมื่อนำ Date object ไป concatenate กับสตริง
  • รูปแบบคงที่ในทุกเบราว์เซอร์ (ตาม ECMAScript spec)
  • ต่างจาก toLocaleString() ที่ขึ้นกับ locale ของผู้ใช้