-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
50 lines (44 loc) · 1.78 KB
/
app.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
var express = require('express');
var app = express();
var port = process.env.PORT || 3000;
app.get('/', function(req, res) {
res.sendFile(__dirname + '/index.htm');
});
app.get('/:id', function(req, res) {
var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
var request = req.params.id;
var unix = request;
var natural = request;
var d, month, day, year, temp;
// if the request is a natural date, set unix to the unix date parsed from natural
if(isNaN(request)) {
// set temp to standard UTC format so unix time is not time zone dependent
d = new Date (request);
// UTC so code is not time zone dependent
month = d.getUTCMonth();
year = d.getUTCFullYear();
day = d.getUTCDate();
temp = year + '-' + month + '-' + day
// divide by 1,000 to convert from ms to seconds
unix = String(Date.parse(temp)/1000);
}
// if the request is unix, set natural to the natural date parsed from unix
else {
// request is in ms, new js date is made in ms, so multiply by 1,000
d = new Date(Number(request) * 1000);
// UTC so code is not time zone dependent
month = d.getUTCMonth();
year = d.getUTCFullYear();
day = d.getUTCDate();
natural = months[month] + ' ' + day + ', ' + year;
}
// if the request is not a number and it fails to be parsed into unix
// or if the request is too high of a unix value
// null will be returned
if(unix === 'NaN' || request > 253402041600) {
res.json({ unix: null, natural: null, request: request});
} else {
res.json({ unix: unix, natural: natural, request: request});
}
});
app.listen(port);