-
Notifications
You must be signed in to change notification settings - Fork 12.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add function to Date prototype and keep other methods #7726
Comments
// Extend the type, and declare the new function
interface Date {
getWeekNumber(): number;
}
// Add the implementation
Date.prototype.getWeekNumber = function () {
....
};
var x = new Date();
x.getWeekNumber(); |
The problem with this is that all the methods of the Date are not available anymore |
See https://github.com/Microsoft/TypeScript/wiki/FAQ#why-did-adding-an-import-or-export-modifier-break-my-program . The |
What is the right way to do it? @RyanCavanaugh |
Take the |
if you are using TS 1.8.5 or higher, consider using: declare global {
interface Date {
myMethod(): number;
}
} see https://github.com/Microsoft/TypeScript/wiki/What%27s-new-in-TypeScript#augmenting-globalmodule-scope-from-modules for more details. |
How to access the "date object" ? Using ... some angular2 stuff here ...
// DATE EXTENSIONS
// ================
declare global {
interface Date {
isToday(): boolean;
}
}
Date.prototype.isToday = (): boolean => {
let today = new Date();
console.log(this); // WILL RETURN FIRST EXPORTED ANGULAR2 OBJECT IN THIS FILE
return today.getFullYear() === this.getFullYear() && today.getMonth() === this.getMonth() && today.getDate() === this.getDate();
}; same behavior on top of the file. EDIT: tried in a separate file: date.extensions.ts export {}
// DATE EXTENSIONS
// ================
declare global {
interface Date {
addDays(days: number, useThis?: boolean): Date;
isToday(): boolean;
clone(): Date;
isAnotherMonth(date: Date): boolean;
isWeekend(): boolean;
isSameDate(date: Date): boolean;
}
}
Date.prototype.addDays = (days: number): Date => {
if (!days) return this;
console.log(this);
let date = this;
date.setDate(date.getDate() + days);
return date;
};
Date.prototype.isToday = (): boolean => {
let today = new Date();
return this.isSameDate(today);
};
Date.prototype.clone = (): Date => {
return new Date(+this);
};
Date.prototype.isAnotherMonth = (date: Date): boolean => {
return date && this.getMonth() !== date.getMonth();
};
Date.prototype.isWeekend = (): boolean => {
return this.getDay() === 0 || this.getDay() === 6;
};
Date.prototype.isSameDate = (date: Date): boolean => {
return date && this.getFullYear() === date.getFullYear() && this.getMonth() === date.getMonth() && this.getDate() === date.getDate();
};
// EOF date-picker.component.ts .. imports ..
import './date.extensions';
.. angular2 stuff ..
.. trying to use the extended methods in here ! .. see the following error message in console:
and logging |
Lambda expressions do not have a use a function expression instead. e.g.: Date.prototype.isToday = function (): boolean {
...
}; |
@mhegazy Thanks alot!! Took me many hours to play around but no success.. |
I figured the lambda issue already. Still didn't work. But thank you gfrSE. Adding export {} on top resolved my issue. |
@mhegazy do you have documentation on that... it is great informaiton and would like to read more up on it.
|
Hello,
I want to add a function to the Date prototype. I want to add a weekNumber function to it that returns the number of the week.
I wan to create make it on the typescript way so much as posible.
This is the prototype function:
I created a interface but the problem was that he removed all the functions of the Date and becease of that my IDE was giving an error that the functions are not existing.
Does someknow how to do it by the right way?`
The text was updated successfully, but these errors were encountered: