-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeconvert.js
37 lines (35 loc) · 881 Bytes
/
timeconvert.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
const month = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
const timeconvert = val => {
val = val.replace(/\//g, " ").trim();
let result = null;
if(/^-?[0-9]+$/.test(val)){
let natural = null;
let d = new Date(Number(val));
let date = month[d.getMonth()] + " " + d.getDate() + ", " + d.getFullYear();
result = JSON.stringify({
"unix": val,
"natural": date
});
}else if(/^[a-zA-Z]+\s[0-9]{2},\s[0-9]{4}$/g.test(val)){
let unix = null;
let parsed = Date.parse(val);
if(isNaN(parsed)){
result = JSON.stringify({
"unix": null,
"natural": null
});
}else{
result = JSON.stringify({
"unix": parsed,
"natural": val
});
}
}else{
result = JSON.stringify({
"unix": null,
"natural": null
});
}
return result;
}
module.exports = timeconvert;