summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/lib/utils/datetime_utility.js
diff options
context:
space:
mode:
authorKushal Pandya <kushalspandya@gmail.com>2019-01-08 18:40:41 +0530
committerKushal Pandya <kushalspandya@gmail.com>2019-01-08 19:41:57 +0530
commit2bb73dab72c4a9c1518d04814b697b8f2dff3194 (patch)
tree897cd0002d8483df09899a610cd9b7a9e2bebec8 /app/assets/javascripts/lib/utils/datetime_utility.js
parent214d741ba5caf1e533873bba1b8f030e80774d72 (diff)
downloadgitlab-ce-2bb73dab72c4a9c1518d04814b697b8f2dff3194.tar.gz
Add `newDate` helper, update `getTimeframeWindowFrom`
Adds `newDate` helper to copy date object without keeping reference. Adds negative length support in `getTimeframeWindowFrom` to go back in time.
Diffstat (limited to 'app/assets/javascripts/lib/utils/datetime_utility.js')
-rw-r--r--app/assets/javascripts/lib/utils/datetime_utility.js34
1 files changed, 27 insertions, 7 deletions
diff --git a/app/assets/javascripts/lib/utils/datetime_utility.js b/app/assets/javascripts/lib/utils/datetime_utility.js
index 59007d5950e..01dbbb9dd16 100644
--- a/app/assets/javascripts/lib/utils/datetime_utility.js
+++ b/app/assets/javascripts/lib/utils/datetime_utility.js
@@ -8,6 +8,14 @@ import { languageCode, s__ } from '../../locale';
window.timeago = timeago;
/**
+ * This method allows you to create new Date instance from existing
+ * date instance without keeping the reference.
+ *
+ * @param {Date} date
+ */
+export const newDate = date => (date instanceof Date ? new Date(date.getTime()) : new Date());
+
+/**
* Returns i18n month names array.
* If `abbreviated` is provided, returns abbreviated
* name.
@@ -321,23 +329,35 @@ export const getSundays = date => {
/**
* Returns list of Dates representing a timeframe of months from startDate and length
+ * This method also supports going back in time when `length` is negative number
*
- * @param {Date} startDate
+ * @param {Date} initialStartDate
* @param {Number} length
*/
-export const getTimeframeWindowFrom = (startDate, length) => {
- if (!(startDate instanceof Date) || !length) {
+export const getTimeframeWindowFrom = (initialStartDate, length) => {
+ if (!(initialStartDate instanceof Date) || !length) {
return [];
}
+ const startDate = newDate(initialStartDate);
+ const moveMonthBy = length > 0 ? 1 : -1;
+
+ startDate.setDate(1);
+ startDate.setHours(0, 0, 0, 0);
+
// Iterate and set date for the size of length
// and push date reference to timeframe list
- const timeframe = new Array(length)
- .fill()
- .map((val, i) => new Date(startDate.getFullYear(), startDate.getMonth() + i, 1));
+ const timeframe = new Array(Math.abs(length)).fill().map(() => {
+ const currentMonth = startDate.getTime();
+ startDate.setMonth(startDate.getMonth() + moveMonthBy);
+ return new Date(currentMonth);
+ });
// Change date of last timeframe item to last date of the month
- timeframe[length - 1].setDate(totalDaysInMonth(timeframe[length - 1]));
+ // when length is positive
+ if (length > 0) {
+ timeframe[timeframe.length - 1].setDate(totalDaysInMonth(timeframe[timeframe.length - 1]));
+ }
return timeframe;
};