2020-04-14 11:28:33 +02:00
|
|
|
import { Injectable } from '@angular/core';
|
2021-11-17 16:25:24 +01:00
|
|
|
import { defaultTimeOfDay } from '../../../../mocks/old-stuff/config/defaultConfigs';
|
|
|
|
import { DateChoice } from '../models/dateChoice.model';
|
2021-11-22 15:56:43 +01:00
|
|
|
import { environment } from '../../../environments/environment';
|
2020-02-05 11:19:03 +01:00
|
|
|
|
|
|
|
@Injectable({
|
2020-04-21 10:50:26 +02:00
|
|
|
providedIn: 'root',
|
2020-02-05 11:19:03 +01:00
|
|
|
})
|
2021-04-29 10:45:22 +02:00
|
|
|
export class DateUtilitiesService {
|
2020-04-21 10:50:26 +02:00
|
|
|
/**
|
2021-05-18 12:12:08 +02:00
|
|
|
* add some dateChoices to a date, to compute intervals
|
2020-04-21 10:50:26 +02:00
|
|
|
* @param days
|
|
|
|
* @param date
|
|
|
|
*/
|
|
|
|
addDaysToDate(days: number, date: Date) {
|
|
|
|
date = new Date(date.valueOf());
|
|
|
|
date.setDate(date.getDate() + days);
|
|
|
|
return date;
|
|
|
|
}
|
2020-02-05 11:13:36 +01:00
|
|
|
|
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({
|
2021-05-17 15:25:22 +02:00
|
|
|
literal: this.formateDateToInputStringNg(d1),
|
2020-04-21 10:50:26 +02:00
|
|
|
date_object: d1,
|
|
|
|
});
|
|
|
|
d1.setDate(d1.getDate() + interval);
|
|
|
|
}
|
2020-04-21 17:26:25 +02:00
|
|
|
return [...dates];
|
2020-04-21 10:50:26 +02:00
|
|
|
}
|
2020-02-05 11:13:36 +01:00
|
|
|
|
2020-04-21 10:50:26 +02:00
|
|
|
/**
|
2021-05-18 12:12:08 +02:00
|
|
|
* get the number of dateChoices between two dates
|
2020-04-21 10:50:26 +02:00
|
|
|
* @param d1
|
|
|
|
* @param d2
|
|
|
|
*/
|
2020-11-06 16:15:42 +01:00
|
|
|
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-02-05 11:13:36 +01: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
|
|
|
|
*/
|
2021-05-17 15:25:22 +02:00
|
|
|
formateDateToInputStringNg(date: Date): string {
|
2020-04-21 10:50:26 +02:00
|
|
|
return [
|
|
|
|
date.getFullYear(),
|
|
|
|
this.getDoubleDigits(date.getMonth() + 1),
|
|
|
|
this.getDoubleDigits(date.getDate()),
|
|
|
|
].join('-');
|
|
|
|
}
|
2020-02-05 11:13:36 +01:00
|
|
|
|
2021-05-17 15:25:22 +02:00
|
|
|
/**
|
|
|
|
* transform a date object to a short date string
|
|
|
|
* @param inputDate
|
|
|
|
*/
|
|
|
|
parseInputDateToDateObject(inputDate: Date): Date {
|
|
|
|
const boom = inputDate.toISOString().substring(0, 10).split('-');
|
2020-11-06 16:15:42 +01:00
|
|
|
|
|
|
|
const converted = new Date(boom['0'], boom['1'] - 1, boom['2']);
|
|
|
|
console.log('converted', converted);
|
|
|
|
return converted;
|
|
|
|
}
|
|
|
|
|
2021-05-17 15:25:22 +02:00
|
|
|
/**
|
|
|
|
* get double in string
|
|
|
|
* @param str
|
|
|
|
*/
|
2020-04-21 10:50:26 +02:00
|
|
|
getDoubleDigits(str) {
|
|
|
|
return ('00' + str).slice(-2);
|
|
|
|
}
|
2020-11-06 14:33:20 +01:00
|
|
|
|
2021-05-17 15:25:22 +02:00
|
|
|
/**
|
2021-05-18 12:12:08 +02:00
|
|
|
* compute the number of dateChoices in the date interval
|
2021-05-17 15:25:22 +02:00
|
|
|
* @param startDateInterval
|
|
|
|
* @param endDateInterval
|
|
|
|
*/
|
2020-11-06 14:33:20 +01:00
|
|
|
countDays(startDateInterval: Date, endDateInterval: Date): number {
|
|
|
|
if (endDateInterval && startDateInterval) {
|
|
|
|
return this.dayDiff(endDateInterval, startDateInterval);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2021-05-17 15:25:22 +02:00
|
|
|
|
2021-11-17 16:25:24 +01:00
|
|
|
makeDefaultCalendarDateChoices(): Date[] {
|
|
|
|
return [
|
|
|
|
this.addDaysToDate(1, new Date()),
|
|
|
|
this.addDaysToDate(2, new Date()),
|
|
|
|
this.addDaysToDate(3, new Date()),
|
|
|
|
];
|
|
|
|
}
|
2021-05-17 15:25:22 +02:00
|
|
|
/**
|
2021-05-18 12:12:08 +02:00
|
|
|
* fill default dates for today + the next 3 dateChoices
|
2021-05-17 15:25:22 +02:00
|
|
|
*/
|
|
|
|
makeDefaultDateChoices(): DateChoice[] {
|
|
|
|
const today = new Date();
|
2021-05-20 12:34:49 +02:00
|
|
|
const ladate2 = this.addDaysToDate(1, today);
|
|
|
|
const ladate3 = this.addDaysToDate(2, today);
|
|
|
|
const ladate4 = this.addDaysToDate(3, today);
|
2021-05-17 15:25:22 +02:00
|
|
|
|
|
|
|
return [
|
2021-11-17 16:52:16 +01:00
|
|
|
this.convertDateToDateChoiceObject(ladate2),
|
|
|
|
this.convertDateToDateChoiceObject(ladate3),
|
|
|
|
this.convertDateToDateChoiceObject(ladate4),
|
2021-05-17 15:25:22 +02:00
|
|
|
];
|
|
|
|
}
|
2021-11-17 16:25:24 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* convert a date to a DateChoice
|
|
|
|
* @param date
|
|
|
|
*/
|
2021-11-17 16:52:16 +01:00
|
|
|
convertDateToDateChoiceObject(date: Date): DateChoice {
|
|
|
|
let isUnder10 = date.getDate() < 10;
|
|
|
|
let day = isUnder10 ? `0${date.getDate()}` : date.getDate();
|
|
|
|
// get month is based on 0, so yeah
|
|
|
|
let input = `${date.getFullYear()}-${date.getMonth() + 1}-${day}`;
|
2021-11-17 16:25:24 +01:00
|
|
|
return {
|
|
|
|
literal: this.formateDateToInputStringNg(date),
|
2021-11-22 15:56:43 +01:00
|
|
|
timeSlices: environment.autofill_default_timeslices ? Object.create(defaultTimeOfDay) : [],
|
2021-11-17 16:25:24 +01:00
|
|
|
date_object: date,
|
2021-11-17 16:52:16 +01:00
|
|
|
date_input: input,
|
2021-11-17 16:25:24 +01:00
|
|
|
};
|
|
|
|
}
|
2020-02-05 11:13:36 +01:00
|
|
|
}
|