import { Injectable } from '@angular/core' ; import { AppLocalizationService } from '@app/shared/common/localization/app-localization.service' ; import { DateTime } from 'luxon' ; @ Injectable ( ) export class DateTimeService { constructor ( private _appLocalizationService ) { } getDate ( ) { if ( abp . clock . provider . supportsMultipleTimezone ) { return DateTime . local ( ) . setZone ( abp . timing . timeZoneInfo . iana . timeZoneId ) ; } else { return DateTime . local ( ) ; } } getUTCDate ( ) { return DateTime . utc ( ) ; } getYear ( ) { return this . getDate ( ) . year ; } getStartOfDay ( ) { return this . getDate ( ) . startOf ( 'day' ) ; } getStartOfDayForDate ( date ) { if ( ! date ) { return date as DateTime ; } if ( date instanceof Date ) { return this . getStartOfDayForDate ( this . fromJSDate ( date ) ) ; } return date . startOf ( 'day' ) ; } getStartOfDayMinusDays ( daysFromNow ) { let date = this . getDate ( ) ; let newDate = this . minusDays ( date , daysFromNow ) ; return this . getStartOfDayForDate ( newDate ) ; } getEndOfDay ( ) { return this . getDate ( ) . endOf ( 'day' ) ; } getEndOfDayForDate ( date ) { if ( ! date ) { return date as DateTime ; } if ( date instanceof Date ) { return this . getEndOfDayForDate ( this . fromJSDate ( date ) ) ; } return date . endOf ( 'day' ) ; } getEndOfDayPlusDays ( daysFromNow ) { let date = this . getDate ( ) ; let newDate = this . plusDays ( date , daysFromNow ) ; return this . getEndOfDayForDate ( newDate ) ; } getEndOfDayMinusDays ( daysFromNow ) { let date = this . getDate ( ) ; let newDate = this . minusDays ( date , daysFromNow ) ; return this . getEndOfDayForDate ( newDate ) ; } plusDays ( date , dayCount ) { if ( date instanceof Date ) { return this . plusDays ( this . fromJSDate ( date ) , dayCount ) ; } return date . plus ( { days : dayCount } ) ; } plusSeconds ( date , seconds ) { if ( ! date ) { return date ; } if ( date instanceof Date ) { return this . plusSeconds ( this . fromJSDate ( date ) , seconds ) ; } return date . plus ( { seconds : seconds } ) ; } minusDays ( date , dayCount ) { return date . minus ( { days : dayCount } ) ; } fromISODateString ( date ) { return DateTime . fromISO ( date ) ; } formatISODateString ( dateText , format ) { let date = this . fromISODateString ( dateText ) ; return date . toFormat ( format ) ; } formatJSDate ( jsDate , format ) { let date = DateTime . fromJSDate ( jsDate ) ; return date . toFormat ( format ) ; } formatDate ( date , format ) { if ( date instanceof Date ) { return this . formatDate ( this . fromJSDate ( date ) , format ) ; } return date . toFormat ( format ) ; } getDiffInSeconds ( maxDate , minDate ) { if ( maxDate instanceof Date && minDate instanceof Date ) { return this . getDiffInSeconds ( this . fromJSDate ( maxDate ) , this . fromJSDate ( minDate ) ) ; } return ( maxDate as DateTime ) . diff ( minDate as DateTime , 'seconds' ) ; } createJSDate ( year , month , day ) { return this . createDate ( year , month , day ) . toJSDate ( ) ; } createDate ( year , month , day ) { if ( abp . clock . provider . supportsMultipleTimezone ) { return DateTime . utc ( year , month + 1 , day ) ; } else { return DateTime . local ( year , month + 1 , day ) ; } } createUtcDate ( year , month , day ) { return DateTime . utc ( year , month + 1 , day ) ; } toUtcDate ( date ) { if ( date instanceof Date ) { return this . createUtcDate ( date . getFullYear ( ) , date . getMonth ( ) , date . getDate ( ) ) . toJSDate ( ) ; } return this . createUtcDate ( date . year , date . month , date . day ) ; } fromJSDate ( date ) { return DateTime . fromJSDate ( date ) ; } fromNow ( date ) { if ( date instanceof Date ) { return this . fromNow ( this . fromJSDate ( date ) ) ; } return date . toRelative ( ) ; } }