File

src/app/config/DateUtilities.ts

Index

Methods

Methods

addDaysToDate
addDaysToDate(days: number, date: Date)

add some days to a date, to compute intervals

Parameters :
Name Type Optional
days number No
date Date No
Returns : any
dayDiff
dayDiff(d1: Date, d2: Date)

get the number of days between two dates

Parameters :
Name Type Optional
d1 Date No
d2 Date No
Returns : Number
formateDate
formateDate(date)

format a date object to the date format used by the inputs of type date YYYY-MM-DD

Parameters :
Name Optional
date No
Returns : any
getDatesInRange
getDatesInRange(d1: Date, d2: Date, interval: number)
Parameters :
Name Type Optional
d1 Date No
d2 Date No
interval number No
Returns : any
getDoubleDigits
getDoubleDigits(str)
Parameters :
Name Optional
str No
Returns : any
import {Injectable} from "@angular/core";

@Injectable({
    providedIn: 'root'
})
export class DateUtilities {

    /**
     * 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;
    };

    /**
     *
     * @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.slice(0);
    }

    /**
     * get the number of days between two dates
     * @param d1
     * @param d2
     */
    dayDiff(d1: Date, d2: Date): Number {
        return Number(((d2.getTime()) - (d1.getTime()) / 31536000000));
    }

    /**
     * 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('-')
    }

    getDoubleDigits(str) {
        return ("00" + str).slice(-2);
    }
}

result-matching ""

    No results matching ""