-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
97 lines (78 loc) · 2.37 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
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
var express = require("express");
var path = require("path");
var app = express();
var NLDate, unix_time;
var result;
var month, day, year;
var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July','August', 'September', 'October', 'November', 'December'];
app.get('/', function (request, response) {
response.sendFile(path.join(__dirname+'/index.html'));
});
app.get('/:query', function (request, response) {
// get argument
var date = request.params.query;
// check if the argument is unixtime (only digits) or natural language date
var format = /[a-z]/i.test(date);
if (!format) {
//console.log('unix format');
unix_time = date;
// convert unix time to date
NLDate = new Date(unix_time * 1000);
// format date to natural language
NLDate = NLDate.toLocaleDateString().split('/');
month = months[NLDate[0] - 1];
day = NLDate[1] + ',';
year = NLDate[2];
NLDate = month + ' ' + day + ' ' + year;
result = {
'unix': +unix_time,
'natural': NLDate
};
} else {
//console.log('not unix format');
// split date arg
date = date.split(' ');
// set date vars
month = date[0];
day = date[1];
year = date[2];
// date validation
if (isMonth(month) === true && isDay(day) === true) {
NLDate = month + ' ' + day + ' ' + year;
// convert date to unix time
unix_time = Date.parse(NLDate) / 1000;
result = {
'unix': unix_time,
'natural': NLDate
};
} else {
//console.log('Not a valid date');
NLDate = 'null';
unix_time = 'null';
result = {
'unix': unix_time,
'natural': NLDate
};
}
}
response.send(result);
});
app.listen(8080, function () {
});
function isMonth(month) {
//console.log('checking if month is valid');
if(months.indexOf(month) != -1) {
return true;
} else {
return false;
}
}
function isDay(day) {
//console.log('checking if day is valid');
day = day.replace(',', '');
if (day > 0 && day < 31) {
return true;
} else {
return false;
}
}