-
Notifications
You must be signed in to change notification settings - Fork 4
/
dateService.js
53 lines (46 loc) · 1.37 KB
/
dateService.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
const date = require('date-fns');
const tz = require('date-fns-tz');
function parseInput(ISOdate) {
const TZ = process.env.TZ;
return tz.utcToZonedTime(ISOdate, TZ);
}
function formatDateToTwoString(date) {
if (date.toString().length == 1) return '0' + date;
return date;
}
function formatHours(ISOdate) {
const TZ = process.env.TZ;
const cardHours = formatDateToTwoString(date.getHours(ISOdate));
const cardMinutes = formatDateToTwoString(date.getMinutes(ISOdate));
let TZOffset = (tz.getTimezoneOffset(TZ) / 3600000).toString();
const TZSymbol = TZOffset.toString()[0] == '-' ? '-' : '+';
if (TZSymbol == '-')
TZOffset = TZOffset.substring(1);
const TZString = `${TZSymbol}${formatDateToTwoString(TZOffset)}:00`;
return `T${cardHours}:${cardMinutes}:00.000${TZString}`;
}
class DateService {
addMonth(ISOdate, hasHours) {
let updatedDate = date
.addMonths(parseInput(ISOdate), 1)
.toISOString()
.toString()
.substring(0, 10);
if (hasHours) {
updatedDate += formatHours(ISOdate);
}
return updatedDate;
}
addDay(ISOdate, hasHours, daysAmount) {
let updatedDate = date
.addDays(parseInput(ISOdate), daysAmount)
.toISOString()
.toString()
.substring(0, 10);
if (hasHours) {
updatedDate += formatHours(ISOdate);
}
return updatedDate;
}
}
module.exports = new DateService();