funky-framadate-front/src/app/core/utils/date-utils.service.ts

51 lines
1.2 KiB
TypeScript

import { Injectable } from '@angular/core';
import * as moment from 'moment';
@Injectable({
providedIn: 'root',
})
export class DateUtilsService {
public static addDaysToDate(days: number, date: Date): Date {
return moment(date).add(days, 'days').toDate();
}
public static diffInDays(dateLeft: Date, dateRight: Date): number {
return moment(dateLeft).diff(moment(dateRight));
}
public static formatDate(date): string {
return moment(date).format('yyyy-MM-dd');
}
public static orderDates(): Date[] {
// TODO: to implement
const datesOrdered: Date[] = [];
return datesOrdered;
}
public static getDatesInRange(d1: Date, d2: Date, interval: number): Date[] {
// TODO: refacto this
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];
}
public static getDoubleDigits(str: string): string {
// TODO: ça sert à quoi ça ?
// Parce que ajouter 2 caractère à une string et ensuite slicer à partir du 2ème caractère, euh…
return ('00' + str).slice(-2);
}
private isInChronologicalOrder(date1: Date, date2: Date): boolean {
return date1 < date2;
}
}