implement FullDate getter, setter, function
getter: date, year, month, day, dayOfWeek setter: year, month, day function: advance
This commit is contained in:
parent
75674df502
commit
7c352d4f37
1 changed files with 31 additions and 11 deletions
|
@ -1,7 +1,7 @@
|
||||||
export class FullDate {
|
export class FullDate {
|
||||||
private date: Date;
|
private _date: Date;
|
||||||
constructor(...argv: any) {
|
constructor(...argv: any) {
|
||||||
this.date = (()=>{
|
this._date = (()=>{
|
||||||
if (argv.length==1) {
|
if (argv.length==1) {
|
||||||
const arg = argv[0];
|
const arg = argv[0];
|
||||||
if (arg instanceof FullDate) return new Date(+arg);
|
if (arg instanceof FullDate) return new Date(+arg);
|
||||||
|
@ -18,7 +18,7 @@ export class FullDate {
|
||||||
})();
|
})();
|
||||||
}
|
}
|
||||||
toString(): string {
|
toString(): string {
|
||||||
const d = this.date;
|
const d = this._date;
|
||||||
const f = (s: any) => ('0'+s).slice(-2);
|
const f = (s: any) => ('0'+s).slice(-2);
|
||||||
return `${d.getFullYear()}-${f(d.getMonth()+1)}-${f(d.getDate())}`;
|
return `${d.getFullYear()}-${f(d.getMonth()+1)}-${f(d.getDate())}`;
|
||||||
}
|
}
|
||||||
|
@ -26,16 +26,36 @@ export class FullDate {
|
||||||
return this.toString();
|
return this.toString();
|
||||||
}
|
}
|
||||||
valueOf(): number {
|
valueOf(): number {
|
||||||
return new Date(this.date).setHours(0, 0, 0, 0);
|
return new Date(this._date).setHours(0, 0, 0, 0);
|
||||||
}
|
}
|
||||||
// prop
|
// getter
|
||||||
getFullYear(): number {
|
get date(): Date {
|
||||||
return this.date.getFullYear();
|
return new Date(this._date);
|
||||||
}
|
}
|
||||||
getMonth(): number {
|
get year(): number {
|
||||||
return this.date.getMonth()+1;
|
return this._date.getFullYear();
|
||||||
}
|
}
|
||||||
getDate(): number {
|
get month(): number {
|
||||||
return this.date.getDate();
|
return this._date.getMonth()+1;
|
||||||
|
}
|
||||||
|
get day(): number {
|
||||||
|
return this._date.getDate();
|
||||||
|
}
|
||||||
|
get dayOfWeek(): number {
|
||||||
|
return this._date.getDay();
|
||||||
|
}
|
||||||
|
// setter
|
||||||
|
set year(val: number) {
|
||||||
|
this._date.setFullYear(val);
|
||||||
|
}
|
||||||
|
set month(val: number) {
|
||||||
|
this._date.setMonth(val-1);
|
||||||
|
}
|
||||||
|
set day(val: number) {
|
||||||
|
this._date.setDate(val);
|
||||||
|
}
|
||||||
|
// func
|
||||||
|
advance(period: number): FullDate {
|
||||||
|
return new FullDate(this._date.valueOf()+period*86400e3);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue