Skip to content
Snippets Groups Projects
Commit c41baed5 authored by Mikkel Aas's avatar Mikkel Aas
Browse files

added error handling

parent fb7849ec
No related branches found
No related tags found
1 merge request!88Nav verifier extended
......@@ -6,13 +6,18 @@ import moment from 'moment';
* @param date2 the second date as a string
* @returns the absolute value of the year difference.
*/
export function computeYearsBetweenDates(date1: string, date2: string): number {
export function computeYearsBetweenDates(date1: string, date2: string): number | Error {
try {
const momentDate1 = moment(date1);
const momentDate2 = moment(date2);
const yearDiff = momentDate1.diff(momentDate2,'years');
return Math.abs(yearDiff);
} catch (error) {
return new Error('unable to calculate amount of years');
}
}
/**
......@@ -21,11 +26,15 @@ export function computeYearsBetweenDates(date1: string, date2: string): number {
* @param date2 the second date as a string
* @returns the absolute value of the month difference.
*/
export function computeMonthsBetweenDates(date1: string, date2: string): number{
export function computeMonthsBetweenDates(date1: string, date2: string): number | Error {
try {
const momentDate1 = moment(date1);
const momentDate2 = moment(date2);
const monthDiff = momentDate1.diff(momentDate2, 'months');
return Math.abs(monthDiff);
} catch (error) {
return new Error('unable to calculate amount of months');
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment