diff --git a/date/date.view.ts b/date/date.view.ts index 367f41a9ec5..0c91b5742f3 100644 --- a/date/date.view.ts +++ b/date/date.view.ts @@ -89,8 +89,8 @@ namespace $.$$ { if( next ) return next - let moment = $mol_try( ()=> new $mol_time_moment( this.value_changed().replace( /\D+$/, '' ) ) ) - if( moment instanceof Error || !moment.year ) return new $mol_time_moment + let moment = $mol_try( ()=> new $mol_time_moment( this.value_changed().replace( /\D+$/, '' ) ).mask( '0000-00' ) ) + if( moment instanceof Error || !moment.year ) return new $mol_time_moment().mask( '0000-00' ) if( moment.month === undefined ) { moment = moment.merge( { month: 0 } ) diff --git a/time/moment/moment.test.ts b/time/moment/moment.test.ts index d79cf5f785b..eaa0ea9e155 100644 --- a/time/moment/moment.test.ts +++ b/time/moment/moment.test.ts @@ -63,6 +63,11 @@ namespace $ { ) } , + 'renormalization'() { + $mol_assert_equal( new $mol_time_moment( '2024-08' ).normal.toString(), '2024-08' ) + $mol_assert_equal( new $mol_time_moment( '2024-11' ).normal.toString(), '2024-11' ) + } , + 'iso week day'() { $mol_assert_equal( new $mol_time_moment( '2017-09-17' ).weekday , $mol_time_moment_weekdays.sunday ) $mol_assert_equal( new $mol_time_moment( '2017-09-18' ).weekday , $mol_time_moment_weekdays.monday ) diff --git a/time/moment/moment.ts b/time/moment/moment.ts index 30d60269599..cbc321c66bc 100644 --- a/time/moment/moment.ts +++ b/time/moment/moment.ts @@ -102,19 +102,29 @@ namespace $ { _native : Date | undefined get native() { + if( this._native ) return this._native - const utc = this.toOffset( 'Z' ) - - return this._native = new Date( Date.UTC( - utc.year ?? 0 , - utc.month ?? 0 , - ( utc.day ?? 0 ) + 1 , - utc.hour ?? 0 , - utc.minute ?? 0 , - utc.second != undefined ? Math.floor( utc.second ) : 0 , - utc.second != undefined ? Math.floor( ( utc.second - Math.floor( utc.second ) ) * 1000 ) : 0 , - ) ) + const second = Math.floor( this.second ?? 0 ) + + const native = new Date( + this.year ?? 0 , + this.month ?? 0 , + ( this.day ?? 0 ) + 1 , + this.hour ?? 0 , + this.minute ?? 0 , + second, + Math.floor( ( ( this.second ?? 0 ) - second ) * 1000 ), + ) + + const offset = native.getTimezoneOffset() + shift: if( this.offset ) { + const target = this.offset.count( 'PT1m' ) + if( target === offset ) break shift + native.setMinutes( native.getMinutes() - offset + target ) + } + + return this._native = native } _normal : $mol_time_moment | undefined @@ -150,9 +160,9 @@ namespace $ { shift( config : $mol_time_duration_config ) { const duration = new $mol_time_duration( config ) const moment = new $mol_time_moment().merge({ - year: this.year, - month: this.month, - day: this.day, + year: this.year ?? 0, + month: this.month ?? 0, + day: this.day ?? 0, hour: this.hour ?? 0, minute: this.minute ?? 0, second: this.second ?? 0, @@ -204,7 +214,7 @@ namespace $ { const duration = new $mol_time_duration( config ) const offset = this.offset || new $mol_time_moment().offset! - let with_time = new $mol_time_moment( 'T00:00:00' ).merge( this ) + let with_time = new $mol_time_moment( '0001-01-01T00:00:00' ).merge( this ) const moment = with_time.shift( duration.summ( offset.mult( -1 ) ) ) return moment.merge({ offset : duration })