import { ServiceKey , ServiceScope } from "@microsoft/sp-core-library" ; import { PageContext } from "@microsoft/sp-page-context" ; const DateHelper_ServiceKey = 'PnPModernSearchDateHelper' ; export class DateHelper { public static ServiceKey : ServiceKey < DateHelper > = ServiceKey . create ( DateHelper_ServiceKey , DateHelper ) ; private momentLibrary : any ; private pageContext : PageContext ; public constructor ( serviceScope ) { serviceScope . whenFinished ( ( ) => { this . pageContext = serviceScope . consume < PageContext > ( PageContext . serviceKey ) ; } ) ; } public async moment ( ) { if ( this . momentLibrary ) { return Promise . resolve ( this . momentLibrary ) ; } else { let moment = await import ( 'moment' ) ; if ( [ 'en-us' , 'en' ] . indexOf ( this . pageContext . cultureInfo . currentUICultureName . toLowerCase ( ) ) === - 1 ) { await import ( `moment/locale/${ this . pageContext . cultureInfo . currentUICultureName . toLowerCase ( ) . split ( '-' ) [ 0 ] }.js` ) ; } this . momentLibrary = moment . default ; this . momentLibrary . locale ( this . pageContext . cultureInfo . currentUICultureName ? this . pageContext . cultureInfo . currentUICultureName : 'en' ) ; return this . momentLibrary ; } } public isDST ( ) { let today = new Date ( ) ; var jan = new Date ( today . getFullYear ( ) , 0 , 1 ) ; var jul = new Date ( today . getFullYear ( ) , 6 , 1 ) ; let stdTimeZoneOffset = Math . max ( jan . getTimezoneOffset ( ) , jul . getTimezoneOffset ( ) ) ; return today . getTimezoneOffset ( ) < stdTimeZoneOffset ; } public addMinutes ( isDst , date , minutes , dst ) { if ( isDst ) { minutes += dst ; } return new Date ( date . getTime ( ) + minutes * 60000 ) ; } }