Skip to content

Commit

Permalink
fix(stopwatch): fix calculate to iso string data
Browse files Browse the repository at this point in the history
  • Loading branch information
abalad committed Nov 21, 2019
1 parent 68d8561 commit 4c1964c
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions projects/truly-ui/src/components/stopwatch/stopwatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,27 @@ export class TlStopwatch implements OnInit, OnDestroy {
@Input('initialTime')
set initialTime(value: string | Date) {
if (typeof value === 'string') {
this.stopWatchService.hour = parseInt( value.substr( 0, 2 ), 10 );
this.stopWatchService.minute = parseInt( value.substr( 3, 2 ), 10 );
this.stopWatchService.second = parseInt( value.substr( 6, 2 ), 10 );
if (value.length === 8) {
this.stopWatchService.hour = parseInt( value.substr( 0, 2 ), 10 );
this.stopWatchService.minute = parseInt( value.substr( 3, 2 ), 10 );
this.stopWatchService.second = parseInt( value.substr( 6, 2 ), 10 );
} else {
const diff = Math.abs(new Date().getTime() - new Date(value).getTime());
const seconds = diff / 1000;
this.stopWatchService.hour = Math.floor(seconds / (60 * 60));
this.stopWatchService.minute = Math.floor( ((seconds % (60 * 60)) / 60));
this.stopWatchService.second = Math.ceil( ((seconds % (60 * 60)) % 60));
}
this.stopWatchService.start();
return;
}

if (value instanceof Date) {
const date = new Date(value);
this.stopWatchService.hour = date.getHours();
this.stopWatchService.minute = date.getMinutes();
this.stopWatchService.second = date.getSeconds();
const diff = Math.abs(new Date().getTime() - new Date(value).getTime());
const seconds = diff / 1000;
this.stopWatchService.hour = Math.floor(seconds / (60 * 60));
this.stopWatchService.minute = Math.floor( ((seconds % (60 * 60)) / 60));
this.stopWatchService.second = Math.ceil( ((seconds % (60 * 60)) % 60));
this.stopWatchService.start();
return;
}
Expand Down

0 comments on commit 4c1964c

Please sign in to comment.