-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
52 lines (48 loc) · 1.21 KB
/
index.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
const express = require('express');
const moment = require('moment');
const app = express();
const bodyparser = require('body-parser');
const { get } = require('lodash');
function getUnixTime(date) {
const unixTime = new Date(date);
console.log('unixTime',Date.parse(unixTime));
return Date.parse(unixTime)/1000;
}
function getNaturalTime(timestamp) {
var options = {
'month': 'long',
'day': '2-digit',
'year': 'numeric',
'timeZone': 'UTC',
};
console.log('timestamp',timestamp);
return new Date(timestamp * 1000).toLocaleDateString('US-EN',options);
}
app.get('/:date', (req, res) => {
const param = get(req.params, 'date');
const re = /[A-Z][a-z]/g;
const isString = re.test(param);
console.log(isString);
if ( param && !isString) {
// console.log('return timestamp');
res.json({
unix: parseInt(param,10),
natural: getNaturalTime(param),
});
return;
} else if (param && isString) {
// console.log('return date');
res.json({
unix: getUnixTime(param),
natural: param,
});
return;
} else {
res.json({
unix: null,
natural: null,
});
return;
}
})
app.listen(3000, () => console.log('Listening on port 3000'));