HTMLModElement: thuộc tính dateTime
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Thuộc tính dateTime của giao diện HTMLModElement là một chuỗi chứa ngày ở định dạng máy đọc được với giá trị thời gian tùy chọn. Nó phản ánh thuộc tính HTML datetime của các phần tử <del> và <ins>.
Giá trị
Một chuỗi. Các định dạng chuỗi hợp lệ, xem phần giá trị hợp lệ của datetime.
Ví dụ
Cho đoạn HTML sau:
<p>The paragraph <del datetime="2021-11-01">has been</del> changed</p>
Chúng ta có thể lấy giá trị của thuộc tính dateTime của phần tử <del>:
const deletedText = document.querySelector("del");
console.log(deletedText.dateTime); // "2021-11-01"
Chúng ta cũng có thể thiết lập thuộc tính dateTime. Dưới đây, chúng ta tạo một phần tử <ins>, sau đó đặt thuộc tính dateTime của phần tử <ins> thành ngày hiện tại theo định dạng YYYY-MM-DD rồi chèn nó sau văn bản đã xóa:
const insertedText = document.createElement("ins");
const now = new Date();
insertedText.dateTime = `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}`;
insertedText.appendChild(document.createTextNode("was"));
deletedText.insertAdjacentElement("afterend", insertedText);
Nếu script của chúng ta chạy vào ngày 9 tháng 1 năm 2025, HTML sẽ như sau:
<p>
The paragraph <del datetime="2021-11-01">has been</del
><ins datetime="2025-1-9">was</ins> changed
</p>
Mô tả kỹ thuật
| Specification |
|---|
| HTML> # dom-mod-datetime> |