-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
110 lines (104 loc) · 3.45 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
const express = require("express");
const cors = require('cors');
const app = express();
const bodyParser = require('body-parser');
const moment = require('moment');
const port = process.env.PORT || 8000;
app.use(express.static('www'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());
function calculardif(datai,dataf,format){
const diffMs = Math.abs(new Date(dataf) - new Date(datai));
const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24));
const diffMonths = Math.floor(diffDays/30);
const diffYears = Math.floor(diffDays/365);
const diffHours = Math.floor((diffMs % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
const diffMinutes = Math.floor((diffMs % (1000 * 60 * 60)) / (1000 * 60));
const diffSeconds = Math.floor((diffMs % (1000 * 60)) / 1000);
let result = {};
switch (format) {
case 'YMdhm':
result = {
years: diffYears,
months: diffMonths,
days: diffDays,
hours: diffHours,
minutes: diffMinutes
};
break;
case 'YMdhms':
result = {
years: diffYears,
months: diffMonths,
days: diffDays,
hours: diffHours,
minutes: diffMinutes,
seconds: diffSeconds
};
break;
case 'YMd':
result = { years: diffYears, months: diffMonths, days: diffDays };
break;
case 'YMdh':
result = { years:diffYears, months: diffMonths, days: diffDays, hours: diffHours };
break;
case 'Y':
result = { years: diffYears };
break;
case 'M':
result = { months: diffMonths };
break;
case 'd':
result = { days: diffDays };
break;
case 'h':
result = { hours: diffHours };
break;
case 'm':
result = { minutes: diffMinutes };
break;
case 's':
result = { seconds: diffSeconds };
break;
case 'dhm':
result = { days: diffDays, hours: diffHours, minutes: diffMinutes };
break;
case 'dhms':
result = { days: diffDays, hours: diffHours, minutes: diffMinutes, seconds: diffSeconds };
break;
case 'dh':
result = { days: diffDays, hours: diffHours };
break;
case 'dm':
result = { days: diffDays, minutes: diffMinutes };
break;
case 'hm':
result = { hours: diffHours, minutes: diffMinutes };
break;
case 'hms':
result = { hours: diffHours, minutes: diffMinutes, seconds: diffSeconds };
break;
default:
result = { error: 'Formato não reconhecido' };
}
return result;
}
app.get("/diferencadatas",(req,res)=>{
const {datai,h1,dataf,h2,format="dhm"} = req.query;
const dateFormat = 'DD/MM/YYYY';
const timeFormat = 'HH:mm';
const dateTime1 = moment(`${datai} ${h1}`, `${dateFormat} ${timeFormat}`);
const dateTime2 = moment(`${dataf} ${h2}`, `${dateFormat} ${timeFormat}`);
if (!datai || !dataf) {
return res.status(400).send('Parâmetros date1 e date2 são obrigatórios.');
}
if (!dateTime1.isValid() || !dateTime2.isValid()) {
return res.status(400).json({ error: 'Formato de data ou hora inválido. Use DD/MM/YYYY para a data e HH:mm para a hora.' });
}
var result = calculardif(dateTime1,dateTime2,format)
return res.json({ result })
})
app.listen(port,()=>{
console.log("AAA")
})