forked from DamandeepS/timestamp-time
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timestamp-time.html
98 lines (91 loc) · 2.88 KB
/
timestamp-time.html
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<link rel="import" href="../polymer/polymer-element.html">
<dom-module id="timestamp-time">
<template>
<style>
:host {
display: block;
}
</style>
{{result}}
</template>
<script>
/**
* `timestamp-time`
* use timestamp to display time
*
* usage `<timestamp-time></timestamp-time>`
*
* ##Attributes
* | Attribute |Datatype| Description |
* |-----------|--------|-------------|
* | utc | Boolean | `true` if you want to get UTC date/time |
* | type | String | `date` or `time` or empty, if we want Date, Time or Full String|
* @customElement
* @polymer
* @demo demo/index.html
*/
class TimestampTime extends Polymer.Element {
static get is() { return 'timestamp-time'; }
static get properties() {
return {
timestamp: {
type: Number,
value: function() {
return Date.now();
}
},
type: {
type: String,
value: ""
},
utc: {
type: Boolean,
default: false
},
result: {
type: String,
computed: '_computeReturn(timestamp, type, utc)'
}
};
}
_computeReturn(timestamp, type, utc) {
if(!timestamp || typeof timestamp != "number") {
return this._computeReturn(Date.now(), type, utc);
}
let date = new Date(timestamp),
locale = "",
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
switch(type.toLowerCase()) {
case "date":
var _date = utc? (months[date.getUTCMonth()]
+ " "
+ date.getUTCDate()
+ ", "
+ date.getUTCFullYear()) : (months[date.getMonth()]
+ " "
+ date.getDate()
+ ", "
+ date.getFullYear());
return _date;
break;
case "time":
var _time = utc? (date.getUTCHours()
+ ":"
+ date.getUTCMinutes()
+ ":"
+ date.getUTCSeconds()) : (date.getHours()
+ ":"
+ date.getMinutes()
+ ":"
+ date.getSeconds());
return _time;
break;
default:
var dateString = utc ? "toGMTString":"toString";
return date[dateString]();
}
}
}
window.customElements.define(TimestampTime.is, TimestampTime);
</script>
</dom-module>