-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsimpletime.js
More file actions
158 lines (149 loc) · 5.99 KB
/
simpletime.js
File metadata and controls
158 lines (149 loc) · 5.99 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
module.exports = function(RED) {
function SimpleTimeNode(config) {
RED.nodes.createNode(this,config);
var node = this;
//node.warn("config object before checking for undefined");
//node.warn(JSON.stringify(config));
this.mydate = (config.mydate === undefined) ? true : config.mydate;
this.myymd = (config.myymd === undefined) ? true : config.myymd;
this.myyear = (config.myyear === undefined) ? true : config.myyear;
this.mymonth = (config.mymonth === undefined) ? true : config.mymonth;
this.mymonthn = (config.mymonthn === undefined) ? true : config.mymonthn;
this.mymonthf = (config.mymonthf === undefined) ? true : config.mymonthf;
this.mydom = (config.mydom === undefined) ? true : config.mydom;
this.mydoy = (config.mydoy === undefined) ? true : config.mydoy;
this.myday = (config.myday === undefined) ? true : config.myday;
this.mydayf = (config.mydayf === undefined) ? true : config.mydayf;
this.myhourpm = (config.myhourpm === undefined) ? true : config.myhourpm;
this.myhour = (config.myhour === undefined) ? true : config.myhour;
this.mytime = (config.mytime === undefined) ? true : config.mytime;
this.mytimes = (config.mytimes === undefined) ? true : config.mytimes;
this.myminute = (config.myminute === undefined) ? true : config.myminute;
this.myminutes = (config.myminutes === undefined) ? true : config.myminutes;
this.mysecond = (config.mysecond === undefined) ? true : config.mysecond;
this.mymillis = (config.mymillis === undefined) ? true : config.mymillis;
this.myepoch = (config.myepoch === undefined) ? true : config.myepoch;
this.myrawdate = (config.myrawdate === undefined) ? true : config.myrawdate;
this.mypm = (config.mypm === undefined) ? true : config.mypm;
this.mysqldt = (config.mysqldt === undefined) ? true : config.mysqldt;
node.on('input', function(msg) {
const monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
const monthNamesFull = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
const dayNames =["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
const dayNamesFull = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
// Check if msg.date exists, otherwise use new Date()
var d
const isDate = (val) => !isNaN(new Date(val).getTime())
if (msg.date == undefined) {
d = new Date()
} else if (isDate(msg.date)) {
d = new Date(msg.date)
} else {
this.warn('msg.date contains an unrecognized date format, please check simpletimes readme for input examples')
return
}
dts = d.toDateString() ;
e = d.getTime();
mnu = pad(d.getMonth()+1, 2);
mnt = (d.getMonth());
mn = (monthNames[mnt]);
mnf = (monthNamesFull[mnt]);
dy = (dayNames[d.getDay()]);
dyf = (dayNamesFull[d.getDay()]);
dt = pad(d.getDate(), 2);
yr = d.getFullYear();
hr = d.getHours();
mi = d.getMinutes();
ny = (Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()) - Date.UTC(d.getFullYear(), 0, 0)) / 86400000;
h = pad(hr, 2);
m = pad(mi, 2);
s = pad(d.getSeconds(), 2);
mil = pad(d.getMilliseconds(), 3);
// Get hour in 12hr format
if (hr===0){thr=12;}
else if (hr>12){thr=hr-12;}
else {thr=hr;}
thr='' + thr;
// Calculate if AM or PM
var amp = ((hr*60)+mi);
if (amp<720){amp="AM";}
else {amp="PM";}
var hm = (h+":"+m);
var hms = (h+":"+m+":"+s);
var ms = (m+":"+s);
if (this.mydate) {
msg.mydate = dts;
};
if (this.myymd) {
msg.myymd = '' + yr + '-' + mnu + '-' + dt;
};
if (this.myyear) {
msg.myyear = ''+yr;
};
if (this.mymonth) {
msg.mymonth = mn;
};
if (this.mymonthf) {
msg.mymonthf = mnf;
};
if (this.mymonthn) {
msg.mymonthn = mnu;
};
if (this.mydom) {
msg.mydom = dt;
};
if (this.mydoy) {
msg.mydoy = ''+ny;
};
if (this.myday) {
msg.myday = dy;
};
if (this.mydayf) {
msg.mydayf = dyf;
};
if (this.myhourpm) {
msg.myhourpm = thr;
};
if (this.myhour) {
msg.myhour = h;
};
if (this.mytime) {
msg.mytime = hm;
};
if (this.mytimes) {
msg.mytimes = hms;
};
if (this.myminute) {
msg.myminute = m;
};
if (this.myminutes) {
msg.myminutes = ms;
};
if (this.mysecond) {
msg.mysecond = s;
};
if (this.mymillis) {
msg.mymillis = mil;
};
if (this.myepoch) {
msg.myepoch = ''+e;
};
if (this.myrawdate) {
msg.myrawdate = d;
};
if (this.mypm) {
msg.mypm = amp;
};
if (this.mysqldt) {
msg.mysqldt = '' + yr + '-' + mnu + '-' + dt + ' ' + hms;
};
node.send(msg);
});
}
function pad(num, size) {
var s = num+"";
while (s.length < size) s = "0" + s;
return s;
}
RED.nodes.registerType("simpletime",SimpleTimeNode);
}