-
Notifications
You must be signed in to change notification settings - Fork 0
/
current-date.js
39 lines (28 loc) · 1.02 KB
/
current-date.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class CurrentDate extends HTMLElement {
constructor() {
super();
this.currentDate = new Date();
}
connectedCallback() {
const shadowRoot = this.attachShadow({ mode: "open" }); // Create Shadow DOM
const options = {
day: "numeric",
month: "long",
year: "numeric",
};
const formattedDate = this.currentDate.toLocaleDateString("en-US", options);
const template = document.getElementById("current-date");
const templateNode = template.content.cloneNode(true);
shadowRoot.appendChild(templateNode);
// insert the date into the shadow DOM.
const dateNode = shadowRoot.getElementById("date");
dateNode.textContent = formattedDate;
}
}
customElements.define("current-date", CurrentDate);
const template = document.getElementById("current-date");
const templateNode = template.content.cloneNode(true);
shadowRoot.appendChild(templateNode);
// insert the date into the shadow DOM.
const dateNode = shadowRoot.getElementById("date");
dateNode.textContent = formattedDate;