Commit 50222d4d authored by Winnie Hellmann's avatar Winnie Hellmann

Add calculateRemainingMilliseconds() helper function

parent 048ec287
......@@ -473,3 +473,15 @@ export const stringifyTime = timeObject => {
*/
export const abbreviateTime = timeStr =>
timeStr.split(' ').filter(unitStr => unitStr.charAt(0) !== '0')[0];
/**
* Calculates the milliseconds between now and a given date string.
* The result cannot become negative.
*
* @param endDate date string that the time difference is calculated for
* @return {number} number of milliseconds remaining until the given date
*/
export const calculateRemainingMilliseconds = endDate => {
const remainingMilliseconds = new Date(endDate).getTime() - Date.now();
return Math.max(remainingMilliseconds, 0);
};
......@@ -352,3 +352,21 @@ describe('prettyTime methods', () => {
});
});
});
describe('calculateRemainingMilliseconds', () => {
beforeEach(() => {
spyOn(Date, 'now').and.callFake(() => new Date('2063-04-04T00:42:00Z').getTime());
});
it('calculates the remaining time for a given end date', () => {
const milliseconds = datetimeUtility.calculateRemainingMilliseconds('2063-04-04T01:44:03Z');
expect(milliseconds).toBe(3723000);
});
it('returns 0 if the end date has passed', () => {
const milliseconds = datetimeUtility.calculateRemainingMilliseconds('2063-04-03T00:00:00Z');
expect(milliseconds).toBe(0);
});
});
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