funky-framadate-front/src/app/features/old-stuff/config/DateUtilities.ts

82 lines
1.7 KiB
TypeScript
Raw Normal View History

import { Injectable } from '@angular/core';
@Injectable({
2020-04-21 10:50:26 +02:00
providedIn: 'root',
})
export class DateUtilities {
2020-04-21 10:50:26 +02:00
/**
* add some days to a date, to compute intervals
* @param days
* @param date
*/
addDaysToDate(days: number, date: Date) {
date = new Date(date.valueOf());
date.setDate(date.getDate() + days);
return date;
}
2020-04-21 10:50:26 +02:00
/**
*
* @param d1
* @param d2
* @param interval
*/
getDatesInRange(d1: Date, d2: Date, interval: number) {
d1 = new Date(d1);
d2 = new Date(d2);
const dates = [];
while (+d1 < +d2) {
dates.push({
literal: this.formateDate(d1),
date_object: d1,
});
d1.setDate(d1.getDate() + interval);
}
return [...dates];
2020-04-21 10:50:26 +02:00
}
2020-04-21 10:50:26 +02:00
/**
* get the number of days between two dates
* @param d1
* @param d2
*/
dayDiff(d1: Date, d2: Date): any {
const div = 1000 * 3600 * 24;
return Math.abs((d2.getTime() - d1.getTime()) / div).toFixed(0);
2020-04-21 10:50:26 +02:00
}
2020-04-21 10:50:26 +02:00
/**
* format a date object to the date format used by the inputs of type date
* YYYY-MM-DD
* @param date
*/
formateDate(date) {
return [
date.getFullYear(),
this.getDoubleDigits(date.getMonth() + 1),
this.getDoubleDigits(date.getDate()),
].join('-');
}
parseInputDateToDateObject(inputDateString: string) {
const boom = inputDateString.split('-');
const converted = new Date(boom['0'], boom['1'] - 1, boom['2']);
console.log('converted', converted);
return converted;
}
2020-04-21 10:50:26 +02:00
getDoubleDigits(str) {
return ('00' + str).slice(-2);
}
countDays(startDateInterval: Date, endDateInterval: Date): number {
// compute the number of days in the date interval
if (endDateInterval && startDateInterval) {
return this.dayDiff(endDateInterval, startDateInterval);
}
return 0;
}
}