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

อ็อบเจ็กต์ Time ของ HTML DOM

อ็อบเจ็กต์ Time ใน HTML DOM แทนองค์ประกอบ <time> ซึ่งใช้แทนวันที่และ/หรือเวลาในรูปแบบที่เครื่องอ่านได้ (machine-readable)

การสร้างองค์ประกอบ <time>

var timeObject = document.createElement("TIME")

คุณสมบัติของ Time Object

คุณสมบัติ คำอธิบาย
dateTimeอ่าน/กำหนดค่าแอตทริบิวต์ dateTime ของ <time>

ตัวอย่างการใช้งาน

ตัวอย่างนี้แสดงวันที่สอบผ่านแอตทริบิวต์ dateTime ของ <time>

<!DOCTYPE html>
<html>
<head>
<title>Time dateTime</title>
<style>
  form { width:70%; margin: 0 auto; text-align: center; }
  * { padding: 2px; margin:5px; }
  input[type="button"] { border-radius: 10px; }
</style>
</head>
<body>
  <form>
    <fieldset>
      <legend>Time-dateTime</legend>
      <label>Notification : <strong>Examination Incoming!</strong> <time id="TimeDateTimeSelect" dateTime="2019-05-23T12:45Z"> </label>
      <input type="button" onclick="getExamDate()" value="What's the Exam Date?">
      <div id="divDisplay"></div>
    </fieldset>
  </form>
<script>
  var divDisplay = document.getElementById("divDisplay");
  var TimeDateTimeSelect = document.getElementById("TimeDateTimeSelect");
  function getExamDate() {
    divDisplay.textContent = 'Examination Date: '+TimeDateTimeSelect.dateTime;
  }
</script>
</body>
</html>

ผลลัพธ์ (Output)

ก่อนกดปุ่ม "What's the Exam Date?":

อ็อบเจ็กต์ Time ของ HTML DOM

หลังกดปุ่ม "What's the Exam Date?":

อ็อบเจ็กต์ Time ของ HTML DOM