|
1
|
|
|
export default { |
|
2
|
|
|
/** |
|
3
|
|
|
* @param {String|Integer} num add a leading 0 to help format dates and times |
|
4
|
|
|
* @returns {String} |
|
5
|
|
|
*/ |
|
6
|
|
|
leadingZero: function (num) { |
|
7
|
29 |
|
return `0${num}`.slice(-2); |
|
8
|
|
|
}, |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Receive a number of seconds and return an string representing the amount of |
|
12
|
|
|
* hours on the format: 00h00m00s |
|
13
|
|
|
* Examples: |
|
14
|
|
|
* beautifySeconds(60, false) => 01m |
|
15
|
|
|
* beautifyMinutes(120, false) => 02m |
|
16
|
|
|
* beautifyMinutes(3900, false) => 1h05m |
|
17
|
|
|
* beautifyMinutes(3900, true) => 1h05m00s |
|
18
|
|
|
* @param {Number} seconds |
|
19
|
|
|
* @param {Boolean} showSeconds |
|
20
|
|
|
* @returns {string} |
|
21
|
|
|
*/ |
|
22
|
|
|
beautifySeconds: function (seconds, showSeconds = true) { |
|
23
|
18 |
|
let response = ""; |
|
24
|
18 |
|
let theTime = { |
|
25
|
|
|
hours: 0, |
|
26
|
|
|
minutes: 0, |
|
27
|
|
|
seconds: 0 |
|
28
|
|
|
}; |
|
29
|
|
|
|
|
30
|
18 |
|
theTime.hours = ~~(seconds / 3600); |
|
31
|
18 |
|
theTime.minutes = ~~((seconds % 3600) / 60); |
|
32
|
18 |
|
theTime.seconds = seconds % 60; |
|
33
|
|
|
|
|
34
|
18 |
|
if (theTime.hours) { |
|
35
|
10 |
|
response += `${theTime.hours}h`; |
|
36
|
10 |
|
response += `${this.leadingZero(theTime.minutes)}m`; |
|
37
|
10 |
|
response += showSeconds ? `${this.leadingZero(theTime.seconds)}s` : ""; |
|
38
|
8 |
|
} else if (theTime.minutes) { |
|
39
|
6 |
|
response += `${this.leadingZero(theTime.minutes)}m`; |
|
40
|
6 |
|
response += showSeconds ? `${this.leadingZero(theTime.seconds)}s` : ""; |
|
41
|
2 |
|
} else if (showSeconds) { |
|
42
|
1 |
|
response += `${this.leadingZero(theTime.seconds)}s`; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
18 |
|
return response; |
|
46
|
|
|
}, |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @see beautifySeconds |
|
50
|
|
|
* Works the same way as beautifySeconds, but receive an amount of minutes |
|
51
|
|
|
* @param {Number} minutes |
|
52
|
|
|
* @param {Boolean} showSeconds |
|
53
|
|
|
* @returns {String} |
|
54
|
|
|
*/ |
|
55
|
|
|
beautifyMinutes: function (minutes, showSeconds = true) { |
|
56
|
10 |
|
return this.beautifySeconds(minutes * 60, showSeconds); |
|
57
|
|
|
}, |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Receives an duration with the format 00h00m00s and returns the amount |
|
62
|
|
|
* of seconds. The inverse of beautifySeconds |
|
63
|
|
|
* |
|
64
|
|
|
* @param {String} theTime |
|
65
|
|
|
* @returns {Number} |
|
66
|
|
|
*/ |
|
67
|
|
|
fromBeutyToSeconds: function (theTime) { |
|
68
|
10 |
|
let response = 0; |
|
69
|
|
|
|
|
70
|
10 |
|
const hRE = /(\d+)h/; |
|
71
|
10 |
|
const mRE = /(\d+)m/; |
|
72
|
10 |
|
const sRE = /(\d+)s/; |
|
73
|
|
|
|
|
74
|
10 |
|
let hours = hRE.exec(theTime); |
|
75
|
10 |
|
let minutes = mRE.exec(theTime); |
|
76
|
10 |
|
let seconds = sRE.exec(theTime); |
|
77
|
|
|
|
|
78
|
10 |
|
if (hours) { |
|
79
|
2 |
|
response += parseInt(hours[1], 10) * 3600; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
10 |
|
if (minutes) { |
|
83
|
5 |
|
response += parseInt(minutes[1], 10) * 60; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
10 |
|
if (seconds) { |
|
87
|
8 |
|
response += parseInt(seconds[1], 10); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
10 |
|
if (!response) { |
|
91
|
|
|
response += parseInt(theTime, 10); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
10 |
|
return response; |
|
95
|
|
|
}, |
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Receive two dates and returns the amount of days between them |
|
100
|
|
|
* Dates on the format supported by the Date constructor |
|
101
|
|
|
* |
|
102
|
|
|
* @param {String} startDateString |
|
103
|
|
|
* @param {String} endDateString |
|
104
|
|
|
* @returns {Number} |
|
105
|
|
|
*/ |
|
106
|
|
|
daysBetween: function (startDateString, endDateString) { |
|
107
|
5 |
|
const oneDay = 24 * 60 * 60 * 1000; |
|
108
|
5 |
|
const startDate = new Date(startDateString); |
|
109
|
5 |
|
const endDate = new Date(endDateString); |
|
110
|
|
|
|
|
111
|
5 |
|
return Math.round( |
|
112
|
|
|
Math.abs((startDate.getTime() - endDate.getTime()) / oneDay) |
|
113
|
|
|
); |
|
114
|
|
|
}, |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Returns the current date on the format: yyyy-mm-dd |
|
118
|
|
|
* @returns {String} |
|
119
|
|
|
*/ |
|
120
|
1 |
|
curdate: () => new Date().toISOString().substr(0, 10), |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Returns the first day of the current month on the format: yyyy-dd-mm |
|
124
|
|
|
* @returns {String} |
|
125
|
|
|
*/ |
|
126
|
|
|
firstDayOfTheMonth: function () { |
|
127
|
|
|
const today = new Date(); |
|
128
|
|
|
return `${today.getFullYear()}-${today.getMonth() + 1}-01`; |
|
129
|
|
|
}, |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Returns the last day of the month on the format: yyyy-mm-dd |
|
133
|
|
|
* It accepts an optional month and year to get the last day of an particular month |
|
134
|
|
|
* @param {String} month |
|
135
|
|
|
* @param {String} year |
|
136
|
|
|
* @returns {String} |
|
137
|
|
|
*/ |
|
138
|
|
|
lastDayOfTheMonth: function (month, year) { |
|
139
|
|
|
const today = new Date(); |
|
140
|
|
|
const useMonth = month ? month : today.getMonth() + 1; |
|
141
|
|
|
const useYear = year ? year : today.getFullYear(); |
|
142
|
|
|
const lastDay = new Date(useYear, useMonth, 0); |
|
143
|
|
|
|
|
144
|
|
|
return `${useYear}-${this.leadingZero(useMonth)}-${this.leadingZero(lastDay.getDate())}`; |
|
145
|
|
|
} |
|
146
|
|
|
}; |
|
147
|
|
|
|