time.ts 1017 Bytes
Newer Older
1
export class Time {
2
3
4
	hour: number;
	minute: number;
	second: number;
5

6
	constructor(hour?: number, minute?: number, second?: number) {
7
		this.hour = hour || 0;
8
9
10
		this.minute = minute || 0;
		this.second = second || 0;
	}
11

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
	isGreater(time: Time): boolean {
		if (this.hour > time.hour) {
			return true;
		} else {
			if (this.minute > time.minute) {
				return true;
			} else {
				if (this.second > time.second) {
					return true;
				}
			}
		}
		return false;
	}
	inSeconds() {
		return this.hour * 60 * 60 + this.minute * 60 + this.second;
	}
mdsecurity's avatar
mdsecurity committed
29
30
	fromSeconds(second: number): Time {
		this.hour = Math.trunc(second / 3600);
31
		second -= this.hour * 3600;
mdsecurity's avatar
mdsecurity committed
32
		this.minute = Math.trunc(second / 60);
33
34
35
36
		second -= this.minute * 60;
		this.second = second;
		return this;
	}
mdsecurity's avatar
mdsecurity committed
37
38
39
40
41
42
43
44
45
46
47
48
49
	toString(): string {
		let minute: any = this.minute;

		if (this.minute === 0) {
			minute = '00';
		}

		if (this.hour > 12) {
			return (this.hour - 12 + ':' + minute + ' PM');
		} else {
			return (this.hour + ':' + minute + ' AM');
		}

50
	}
51
}