Commit d777539a authored by Martin Wortschack's avatar Martin Wortschack

Merge branch 'add-get-date-in-future-util' into 'master'

Add getDateInFuture util method

See merge request gitlab-org/gitlab!22671
parents 3c1e0c6d 93c07521
......@@ -555,6 +555,16 @@ export const calculateRemainingMilliseconds = endDate => {
export const getDateInPast = (date, daysInPast) =>
new Date(newDate(date).setDate(date.getDate() - daysInPast));
/**
* Adds a given number of days to a given date and returns the new date.
*
* @param {Date} date the date that we will add days to
* @param {Number} daysInFuture number of days that are added to a given date
* @returns {Date} Date in future as Date object
*/
export const getDateInFuture = (date, daysInFuture) =>
new Date(newDate(date).setDate(date.getDate() + daysInFuture));
/*
* Appending T00:00:00 makes JS assume local time and prevents it from shifting the date
* to match the user's time zone. We want to display the date in server time for now, to
......
---
title: Add getDateInFuture util method
merge_request: 22671
author:
type: added
......@@ -455,6 +455,23 @@ describe('getDateInPast', () => {
});
});
describe('getDateInFuture', () => {
const date = new Date('2019-07-16T00:00:00.000Z');
const daysInFuture = 90;
it('returns the correct date in the future', () => {
const dateInFuture = datetimeUtility.getDateInFuture(date, daysInFuture);
const expectedDateInFuture = new Date('2019-10-14T00:00:00.000Z');
expect(dateInFuture).toStrictEqual(expectedDateInFuture);
});
it('does not modifiy the original date', () => {
datetimeUtility.getDateInFuture(date, daysInFuture);
expect(date).toStrictEqual(new Date('2019-07-16T00:00:00.000Z'));
});
});
describe('getDatesInRange', () => {
it('returns an empty array if 1st or 2nd argument is not a Date object', () => {
const d1 = new Date('2019-01-01');
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment