1
|
|
|
import App from "app"; |
2
|
|
|
|
3
|
|
|
App.helpers.date = { |
4
|
|
|
/** |
5
|
|
|
* @param {String|Integer} num add a leading 0 to help format dates and times |
6
|
|
|
* @returns {String} |
7
|
|
|
*/ |
8
|
|
|
leadingZero(num) { |
9
|
|
|
return `0${num}`.slice(-2); |
10
|
|
|
}, |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Receive a number of seconds and return an string representing the amount of |
14
|
|
|
* hours on the format: 00h00m00s |
15
|
|
|
* Examples: |
16
|
|
|
* beautifySeconds(60, false) => 01m |
17
|
|
|
* beautifyMinutes(120, false) => 02m |
18
|
|
|
* beautifyMinutes(3900, false) => 1h05m |
19
|
|
|
* beautifyMinutes(3900, true) => 1h05m00s |
20
|
|
|
* @param {Number} seconds |
21
|
|
|
* @param {Boolean} showSeconds |
22
|
|
|
* @returns {string} |
23
|
|
|
*/ |
24
|
|
|
beautifySeconds(seconds, showSeconds = true) { |
25
|
|
|
let response = ""; |
26
|
|
|
let theTime = { |
27
|
|
|
hours: 0, |
28
|
|
|
minutes: 0, |
29
|
|
|
seconds: 0 |
30
|
|
|
}; |
31
|
|
|
|
32
|
|
|
theTime.hours = ~~(seconds / 3600); |
33
|
|
|
theTime.minutes = ~~((seconds % 3600) / 60); |
34
|
|
|
theTime.seconds = seconds % 60; |
35
|
|
|
|
36
|
|
|
if (theTime.hours) { |
37
|
|
|
response += `${theTime.hours}h`; |
38
|
|
|
response += `${this.leadingZero(theTime.minutes)}m`; |
39
|
|
|
response += showSeconds ? `${this.leadingZero(theTime.seconds)}s` : ""; |
40
|
|
|
} else if (theTime.minutes) { |
41
|
|
|
response += `${this.leadingZero(theTime.minutes)}m`; |
42
|
|
|
response += `${this.leadingZero(theTime.seconds)}s`; |
43
|
|
|
} else if (showSeconds) { |
44
|
|
|
response += `${this.leadingZero(theTime.seconds)}s`; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return response; |
48
|
|
|
}, |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @see beautifySeconds |
52
|
|
|
* Works the same way as beautifySeconds, but receibe an amount of minutes |
53
|
|
|
* @param {Number} minutes |
54
|
|
|
* @param {Boolean} showSeconds |
55
|
|
|
* @returns {String} |
56
|
|
|
*/ |
57
|
|
|
beautifyMinutes: (minutes, showSeconds = true) => this.beautifySeconds(minutes * 60, showSeconds), |
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(theTime) { |
68
|
|
|
let response = 0; |
69
|
|
|
|
70
|
|
|
const hRE = /(\d+)h/; |
71
|
|
|
const mRE = /(\d+)m/; |
72
|
|
|
const sRE = /(\d+)s/; |
73
|
|
|
|
74
|
|
|
let hours = hRE.exec(theTime); |
75
|
|
|
let minutes = mRE.exec(theTime); |
76
|
|
|
let seconds = sRE.exec(theTime); |
77
|
|
|
|
78
|
|
|
if (hours) { |
79
|
|
|
response += parseInt(hours[1], 10) * 3600; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if (minutes) { |
83
|
|
|
response += parseInt(minutes[1], 10) * 60; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (seconds) { |
87
|
|
|
response += parseInt(seconds[1], 10); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (!response) { |
91
|
|
|
response += parseInt(theTime, 10); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
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(startDateString, endDateString) { |
107
|
|
|
const oneDay = 24 * 60 * 60 * 1000; |
108
|
|
|
const startDate = new Date(startDateString); |
109
|
|
|
const endDate = new Date(endDateString); |
110
|
|
|
|
111
|
|
|
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
|
|
|
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
|
|
|
|