-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
47 lines (36 loc) · 1.03 KB
/
server.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
40
41
42
43
44
45
46
47
var express = require('express'),
moment = require('moment'),
parseFormat = require('moment-parseformat'),
port = process.argv[2];
var app = express();
app.get('/timestamp/:input', function(request, response) {
var input = request.params.input,
output = {unix: null, date: null};
if(isUnixTimestamp(input)) {
output.unix = input;
input = Number(input);
} else if(isDate(input)) {
output.unix = toUnixTimestamp(input);
}
output.date = toDate(input);
response.json(output);
});
function isUnixTimestamp(input) {
return !isNaN(input) && parseInt(Number(input)) == input && input >= 0;
}
function isDate(input) {
if(Number(input) < 0) {
return false;
}
return moment(input, parseFormat(input)).isValid();
}
function toUnixTimestamp(input) {
return moment(input, parseFormat(input)).valueOf();
}
function toDate(input) {
if(isDate(input)) {
return moment(input, parseFormat(input)).toDate();
}
return null;
}
app.listen(port);