Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- https://stackoverflow.com/questions/37096367/how-to-convert-seconds-to-minutes-and-hours-in-javascript
- function secondsToHms(d) {
- d = Number(d);
- var h = Math.floor(d / 3600);
- var m = Math.floor(d % 3600 / 60);
- var s = Math.floor(d % 3600 % 60);
- var hDisplay = h > 0 ? h + (h == 1 ? " hour, " : " hours, ") : "";
- var mDisplay = m > 0 ? m + (m == 1 ? " minute, " : " minutes, ") : "";
- var sDisplay = s > 0 ? s + (s == 1 ? " second" : " seconds") : "";
- return hDisplay + mDisplay + sDisplay;
- }
- class TimeDuration {
- private totalSeconds: number = 0;
- constructor(totalSeconds: number = 0) {
- this.totalSeconds = totalSeconds;
- }
- get TotalSeconds(): number {
- return this.totalSeconds;
- }
- set TotalSeconds(totalSeconds: number) {
- this.totalSeconds = totalSeconds;
- }
- get Hours(): number {
- return Math.floor(this.totalSeconds / 3600);
- }
- get Minutes(): number {
- return Math.floor(this.totalSeconds % 3600 / 60);
- }
- get Seconds(): number {
- return Math.floor(this.totalSeconds % 3600 % 60);
- }
- get ToText(): string {
- const h = this.Hours;
- const m = this.Minutes;
- const s = this.Seconds;
- var hDisplay = h > 0 ? h + (h == 1 ? " hour, " : " hours, ") : "";
- var mDisplay = m > 0 ? m + (m == 1 ? " minute, " : " minutes, ") : "";
- var sDisplay = s > 0 ? s + (s == 1 ? " second" : " seconds") : "";
- return hDisplay + mDisplay + sDisplay;
- }
- public AddHours(hours: number): void {
- this.TotalSeconds += hours * 3600;
- }
- public AddMinutes(minutes: number): void {
- this.TotalSeconds += minutes * 60;
- }
- public AddSeconds(seconds: number): void {
- this.TotalSeconds += seconds;
- }
- }
- const timeDuration = new TimeDuration(300);
- // timeDuration.TotalSeconds = 3030;
- console.log(timeDuration.TotalSeconds);
- console.log(timeDuration.Hours);
- console.log(timeDuration.Minutes);
- console.log(timeDuration.Seconds);
- console.log(timeDuration.ToText);
- timeDuration.AddHours(1);
- console.log(timeDuration.ToText);
- timeDuration.AddMinutes(1);
- console.log(timeDuration.ToText);
- timeDuration.AddSeconds(1);
- console.log(timeDuration.ToText);
Advertisement
Add Comment
Please, Sign In to add comment