1
+ <!--
2
+
3
+ Implementation of a web component that enriches the functionality of regular <time> elements
4
+ to support locale specific formatting.
5
+
6
+ <time is='my-time' datestyle='' timestyle"='' datetime=''>
7
+
8
+ datetime
9
+
10
+ https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dl
11
+
12
+ <dl>
13
+ <dt><code>datetime</code></dt>
14
+ <dd><p>...</p></dd>
15
+ </dl>
16
+
17
+
18
+ (description)
19
+
20
+ ## HTML Attributes
21
+
22
+ ## JavaScript accessible attributes
23
+
24
+ https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_custom_elements
25
+
26
+ https://developer.mozilla.org/en-US/docs/Web/HTML/Element/time
27
+
28
+ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat
29
+
30
+
31
+ It may represent one of the following:
32
+
33
+ A time on a 24-hour clock.
34
+ A precise date in the Gregorian calendar (with optional time and timezone information).
35
+ A valid time duration.
36
+
37
+ customElements.define("my-time", ExpandingList, { extends: "time" });
38
+
39
+
40
+
41
+ const fmt = new Intl.DateTimeFormat(navigator.languages, {
42
+ dateStyle: this._dateStyle,
43
+ timeStyle: this._timeStyle,
44
+ });
45
+ this.textContent = fmt.format(date);
46
+
47
+
48
+ -->
49
+
50
+ <style>
51
+ time[is="u-time"] {
52
+ color: red;
53
+ }
54
+ </style>
55
+
56
+ <script for="time">
57
+ export default class MyTime extends HTMLTimeElement {
58
+ static observedAttributes = ["datetime", "datestyle", "timestyle"];
59
+
60
+ _value = '2020-01-01';
61
+ _frm = undefined;
62
+ _dateStyle = "medium";
63
+ _timeStyle = "medium";
64
+
65
+
66
+ init() {
67
+ console.debug('init()');
68
+ debugger;
69
+ } // init()
70
+
71
+
72
+ // show the date and time as textContent in the defined style.
73
+ showValue() {
74
+ let date;
75
+
76
+ if (this._value === '') {
77
+ date = new Date();
78
+
79
+ } else if (this._value.match(/^\d+$/)) {
80
+ date = new Date(Number(this._value));
81
+
82
+ } else {
83
+ date = new Date(this._value);
84
+ }
85
+
86
+ const fmt = new Intl.DateTimeFormat(navigator.languages, {
87
+ dateStyle: this._dateStyle,
88
+ timeStyle: this._timeStyle,
89
+ });
90
+ this.textContent = fmt.format(date);
91
+ } // showValue()
92
+
93
+
94
+ attributeChangedCallback(name, oldValue, newValue) {
95
+ console.debug("attributeChanged", name, oldValue, newValue);
96
+
97
+ this.value = this.getAttribute('datetime');
98
+ this._frm = this.getAttribute('frm');
99
+
100
+ if (name === 'datetime') {
101
+ this._value = newValue;
102
+
103
+ } else if (name === 'datestyle') {
104
+ this._dateStyle = newValue.length ? newValue : undefined;
105
+ if ((this._dateStyle === 'none') || (this._dateStyle === 'null')) {
106
+ this._dateStyle = undefined;
107
+ }
108
+
109
+ } else if (name === 'timestyle') {
110
+ this._timeStyle = newValue.length ? newValue : undefined;
111
+ if ((this._timeStyle === 'none') || (this._timeStyle === 'null')) {
112
+ this._timeStyle = undefined;
113
+ }
114
+
115
+ }
116
+ this.showValue();
117
+ } // attributeChangedCallback()
118
+ }
119
+ </script>
0 commit comments