-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexpress-dates-middleware.js
69 lines (52 loc) · 2.31 KB
/
express-dates-middleware.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
'use strict';
var moment = require('moment');
// Midleware for express to convert startDate and endDate request parameters
module.exports = function(req, res, next) {
var startDate,
endDate;
// Function to parse a string into an integer for use as a milisecond epoch
var parseEpoch = function(date) {
// If the date string is just numbers
if (!isNaN(date)) {
// If the length of the sting is then 13, assume it is a epoch based on seconds and not miliseconds
if (date.length < 13) {
// Change the sting into an integer
date = parseInt(date);
// Multiply the integer by 1000 to get the milisecond epoch and reurn the value
return date * 1000;
} else {
// Return the string as an integer
return parseInt(date);
}
} else {
// Just return the sting as it is not an epoch
return date;
}
};
// If we dont have a startDate and endDate, send the current day
if (!req.query.startDate && !req.query.endDate) {
req.query.startDate = new Date(moment().format('L') + ' 00:00:00');
req.query.endDate = new Date(moment().format('L') + ' 23:59:59');
}
// If we have only a start date, return from start date to current day
else if (req.query.startDate && !req.query.endDate) {
startDate = parseEpoch(req.query.startDate);
req.query.startDate = new Date(startDate);
req.query.endDate = new Date(moment().format('L') + ' 23:59:59');
}
// If we only have an end date, return only that day
else if (!req.query.startDate && req.query.endDate) {
endDate = parseEpoch(req.query.endDate);
req.query.startDate = new Date(moment(new Date(endDate)).format('L') + ' 00:00:00');
req.query.endDate = new Date(moment(new Date(endDate)).format('L') + ' 23:59:59');
}
// If we have both a startDate and endDate, convert to Date objects
else if (req.query.startDate && req.query.endDate) {
startDate = parseEpoch(req.query.startDate);
endDate = parseEpoch(req.query.endDate);
req.query.startDate = new Date(startDate);
req.query.endDate = new Date(endDate);
}
// keep executing the router middleware
next();
};