Last active
October 15, 2019 17:33
-
-
Save netstart/c92e09730f3675ba8fb33be48520a86d to your computer and use it in GitHub Desktop.
prototype extention objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Import on top of app.module.ts to use across the application | |
* | |
* import './shared/utils/date.prototype.extendions'; | |
*/ | |
declare global { | |
interface Date { | |
addDays(days: number, useThis?: boolean): Date; | |
addSeconds(seconds: number): Date; | |
addMinutes(minutes: number): Date; | |
addHours(hours: number): Date; | |
addMonths(months: number): Date; | |
isToday(): boolean; | |
clone(): Date; | |
isAnotherMonth(date: Date): boolean; | |
isWeekend(): boolean; | |
isSameDate(date: Date): boolean; | |
getStringDate(): string; | |
} | |
} | |
Date.prototype.addDays = function(days: number): Date { | |
if (!days) { | |
return this; | |
} | |
this.setDate(this.getDate() + days); | |
return this; | |
}; | |
Date.prototype.addSeconds = function(seconds: number) { | |
let value = this.valueOf(); | |
value += 1000 * seconds; | |
return new Date(value); | |
}; | |
Date.prototype.addMinutes = function(minutes: number) { | |
let value = this.valueOf(); | |
value += 60000 * minutes; | |
return new Date(value); | |
}; | |
Date.prototype.addHours = function(hours: number) { | |
let value = this.valueOf(); | |
value += 3600000 * hours; | |
return new Date(value); | |
}; | |
Date.prototype.addMonths = function(months: number) { | |
const value = new Date(this.valueOf()); | |
let mo = this.getMonth(); | |
let yr = this.getYear(); | |
mo = (mo + months) % 12; | |
if (0 > mo) { | |
yr += (this.getMonth() + months - mo - 12) / 12; | |
mo += 12; | |
} else { | |
yr += ((this.getMonth() + months - mo) / 12); | |
} | |
value.setMonth(mo); | |
value.setFullYear(yr); | |
return value; | |
}; | |
Date.prototype.isToday = function(): boolean { | |
const today = new Date(); | |
return this.isSameDate(today); | |
}; | |
Date.prototype.clone = function(): Date { | |
return new Date(+this); | |
}; | |
Date.prototype.isAnotherMonth = function(date: Date): boolean { | |
return date && this.getMonth() !== date.getMonth(); | |
}; | |
Date.prototype.isWeekend = function(): boolean { | |
return this.getDay() === 0 || this.getDay() === 6; | |
}; | |
Date.prototype.isSameDate = function(date: Date): boolean { | |
return date && this.getFullYear() === date.getFullYear() && this.getMonth() === date.getMonth() && this.getDate() === date.getDate(); | |
}; | |
Date.prototype.getStringDate = function(): string { | |
// Month names in Brazilian Portuguese | |
const monthNames = ['Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro']; | |
// Month names in English | |
// let monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; | |
const today = new Date(); | |
if (this.getMonth() === today.getMonth() && this.getDay() === today.getDay()) { | |
return 'Hoje'; | |
// return "Today"; | |
} else if (this.getMonth() === today.getMonth() && this.getDay() === today.getDay() + 1) { | |
return 'Amanhã'; | |
// return "Tomorrow"; | |
} else if (this.getMonth() === today.getMonth() && this.getDay() === today.getDay() - 1) { | |
return 'Ontem'; | |
// return "Yesterday"; | |
} else { | |
return this.getDay() + ' de ' + this.monthNames[this.getMonth()] + ' de ' + this.getFullYear(); | |
// return this.monthNames[this.getMonth()] + ' ' + this.getDay() + ', ' + this.getFullYear(); | |
} | |
}; | |
export {}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment