49 lines
1.3 KiB
TypeScript
49 lines
1.3 KiB
TypeScript
|
|
interface Number {
|
|
pad(n: number, char?: string): string
|
|
}
|
|
|
|
Number.prototype.pad = function(n, char = "0") {
|
|
return (new Array(n).join(char) + this).slice(-n)
|
|
}
|
|
|
|
interface Date {
|
|
toFrDate(): string,
|
|
toFrTime(): string,
|
|
toSmallFrDate(): string,
|
|
difference(start_at?: Date): string,
|
|
}
|
|
|
|
Date.prototype.toFrDate = function() {
|
|
return `${this.getDate()}/${Number(this.getMonth() + 1).pad(2)}/${this.getFullYear()}`
|
|
}
|
|
|
|
Date.prototype.toFrTime = function(separator = 'h') {
|
|
return `${this.getHours().pad(2, '0')}${separator}${Number(this.getMinutes()).pad(2)}`
|
|
}
|
|
|
|
Date.prototype.toSmallFrDate = function() {
|
|
if ((new Date(this)).toFrDate() === (new Date()).toFrDate()) {
|
|
return `${this.toFrTime()}`
|
|
}
|
|
|
|
return `${this.toFrDate()} ${this.toFrTime()}`
|
|
}
|
|
|
|
Date.prototype.difference = function (start_at) {
|
|
if (!this) {
|
|
return '--:--'
|
|
}
|
|
let end_at = this
|
|
if (!start_at) {
|
|
end_at = new Date()
|
|
start_at = this
|
|
}
|
|
let timer = Math.floor((end_at.getTime() - start_at.getTime()) / 1000)
|
|
let hours = Math.floor(timer / 3600)
|
|
let minutes = Math.floor((timer - hours * 3600) / 60)
|
|
let secondes = timer - hours * 3600 - minutes * 60
|
|
return `${hours}:${String(minutes).padStart(2, '0')}:${String(secondes).padStart(2, '0')}`
|
|
|
|
}
|