import { Injectable } from '@angular/core' ; @ Injectable ( ) export class DateUtils { getDateFormatApp ( date , ignoreTimeZone = true ) { } /**Range dates utils */ public getRange ( range ) { switch ( range ) { case 'all' : return this . allDates ( ) ; case 'yesterday' : return this . setYesterday ( ) ; case 'beforeYesterday' : return this . setBeforeYesterday ( ) ; case 'weekStart' : return this . setWeekStart ( ) ; case 'monthStart' : return this . setMonthStart ( ) ; case 'pastMonth' : return this . setPastMonth ( ) ; case 'pastMonthFull' : return this . setPastMonthFull ( ) ; case 'monthStartPreviousYear' : return this . setMonthStartPreviousYear ( ) ; case 'monthFullPreviousYear' : return this . setMonthFullPreviousYear ( ) ; case 'yearStart' : return this . setYearStart ( ) ; case 'yearStartPreviousYear' : return this . setYearStartPreviousYear ( ) ; case 'last7' : return this . setLast7 ( ) ; case 'last15' : return this . setLast15 ( ) ; case 'last30' : return this . setLast30 ( ) ; case 'last60' : return this . setLast60 ( ) ; case 'last120' : return this . setLast120 ( ) ; } } public allDates ( ) { return [ new Date ( '1984-08-01' ) , new Date ( '2090-01-01' ) ] ; } public setYesterday ( ) { const d = new Date ( ) ; const yesterday = d . setDate ( d . getDate ( ) - 1 ) ; return [ new Date ( yesterday ) , new Date ( yesterday ) ] ; } public setBeforeYesterday ( ) { const d = new Date ( ) ; const beforeYesterday = d . setDate ( d . getDate ( ) - 2 ) ; return [ new Date ( beforeYesterday ) , new Date ( beforeYesterday ) ] ; } public setWeekStart ( ) { const getMonday = ( d ) => { d = new Date ( d ) ; var day = d . getDay ( ) , diff = d . getDate ( ) - day + ( day == 0 ? - 6 : 1 ) ; return new Date ( d . setDate ( diff ) ) ; } ; const today = new Date ( ) ; const monday = getMonday ( new Date ( ) ) ; return [ monday , today ] ; } public setMonthStart ( ) { const today = new Date ( ) ; const monthStart = new Date ( today . getFullYear ( ) , today . getMonth ( ) , 1 ) ; return [ monthStart , today ] ; } public setPastMonth ( ) { const t = new Date ( ) ; var d = new Date ( ) ; var newMonth = d . getMonth ( ) - 1 ; if ( newMonth < 0 ) { newMonth += 12 ; d . setFullYear ( d . getFullYear ( ) - 1 ) ; } d . setMonth ( newMonth ) ; const monthStart = new Date ( d . getFullYear ( ) , d . getMonth ( ) , 1 ) ; const monthEnd = new Date ( d . getFullYear ( ) , d . getMonth ( ) , t . getDate ( ) ) ; return [ monthStart , monthEnd ] ; } public setPastMonthFull ( ) { const t = new Date ( ) ; var d = new Date ( ) ; var newMonth = d . getMonth ( ) - 1 ; if ( newMonth < 0 ) { newMonth += 12 ; d . setFullYear ( d . getFullYear ( ) - 1 ) ; } d . setMonth ( newMonth ) ; const monthStart = new Date ( d . getFullYear ( ) , d . getMonth ( ) , 1 ) ; const monthEnd = new Date ( d . getFullYear ( ) , d . getMonth ( ) , new Date ( d . getFullYear ( ) , d . getMonth ( ) + 1 , 0 ) . getDate ( ) ) ; return [ monthStart , monthEnd ] ; } public setMonthStartPreviousYear ( ) { const t = new Date ( ) ; const today = new Date ( t . getFullYear ( ) - 1 , t . getMonth ( ) , t . getDate ( ) ) ; const monthStart = new Date ( today . getFullYear ( ) , today . getMonth ( ) , 1 ) ; return [ monthStart , today ] ; } public setMonthFullPreviousYear ( ) { const t = new Date ( ) ; const monthStart = new Date ( t . getFullYear ( ) - 1 , t . getMonth ( ) , 1 ) ; const today = new Date ( t . getFullYear ( ) - 1 , t . getMonth ( ) , new Date ( t . getFullYear ( ) , t . getMonth ( ) + 1 , 0 ) . getDate ( ) ) ; return [ monthStart , today ] ; } public setYearStart ( ) { const today = new Date ( ) ; const yearStart = new Date ( today . getFullYear ( ) , 0 , 1 ) ; return [ yearStart , today ] ; } public setYearStartPreviousYear ( ) { const t = new Date ( ) ; const today = new Date ( t . getFullYear ( ) - 1 , t . getMonth ( ) , t . getDate ( ) ) ; const yearStart = new Date ( today . getFullYear ( ) , 0 , 1 ) ; return [ yearStart , today ] ; } public setLast7 ( ) { const today = new Date ( ) ; const last7 = new Date ( today . getTime ( ) - ( 6 * 24 * 60 * 60 * 1000 ) ) ; return [ last7 , today ] ; } public setLast15 ( ) { const today = new Date ( ) ; const last15 = new Date ( today . getTime ( ) - ( 14 * 24 * 60 * 60 * 1000 ) ) ; return [ last15 , today ] ; } public setLast30 ( ) { const today = new Date ( ) ; const last15 = new Date ( today . getTime ( ) - ( 29 * 24 * 60 * 60 * 1000 ) ) ; return [ last15 , today ] ; } public setLast60 ( ) { const today = new Date ( ) ; const last15 = new Date ( today . getTime ( ) - ( 59 * 24 * 60 * 60 * 1000 ) ) ; return [ last15 , today ] ; } public setLast120 ( ) { const today = new Date ( ) ; const last15 = new Date ( today . getTime ( ) - ( 119 * 24 * 60 * 60 * 1000 ) ) ; return [ last15 , today ] ; } public rangeToString ( range ) { const dtf = new Intl . DateTimeFormat ( 'en' , { year : 'numeric' , month : '2-digit' , day : '2-digit' } ) ; if ( ! range [ 1 ] ) { range [ 1 ] = range [ 0 ] ; } let stringRange = [ range [ 0 ] , range [ 1 ] ] . map ( date => { let [ { value : mo } , , { value : da } , , { value : ye } ] = dtf . formatToParts ( date ) ; return `${ ye }-${ mo }-${ da }` ; } ) ; return stringRange ; } }