Skip to content
This repository has been archived by the owner on Nov 21, 2024. It is now read-only.

Commit

Permalink
feat(base-resource.model): Add format date in methods for work with d…
Browse files Browse the repository at this point in the history
…ates
  • Loading branch information
EndyKaufman committed Sep 3, 2017
1 parent 48702e4 commit 0c0b018
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/base/base-models/base-resource.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,15 @@ export class BaseResourceModel {
}
return obj;
}
dateAsString(fieldName: string) {
dateAsString(fieldName: string, dateAsStringFormat?: string) {
if (dateAsStringFormat === undefined) {
dateAsStringFormat = this.dateAsStringFormat;
}
let text: string;
text = '';
if (this[fieldName] !== undefined) {
try {
text = moment(this[fieldName]).format(this.dateAsStringFormat);
text = moment(this[fieldName]).format(dateAsStringFormat);
} catch (err) {
text = '';
}
Expand All @@ -140,13 +143,16 @@ export class BaseResourceModel {
}
return text;
}
setDateAsString(fieldName: string, value: any) {
setDateAsString(fieldName: string, value: any, dateAsStringFormat?: string) {
if (dateAsStringFormat === undefined) {
dateAsStringFormat = this.dateAsStringFormat;
}
if (this[fieldName] === undefined) {
this[fieldName] = null;
return;
}
try {
value = moment(value, this.dateAsStringFormat).toDate();
value = moment(value, dateAsStringFormat).toDate();
} catch (err) {
value = null;
}
Expand All @@ -155,14 +161,17 @@ export class BaseResourceModel {
}
this[fieldName] = value;
}
getDateInput(fieldName: string) {
getDateInput(fieldName: string, dateInputFormat?: string) {
if (dateInputFormat === undefined) {
dateInputFormat = this.dateInputFormat;
}
if (this[fieldName] === undefined) {
return '';
}
let value: string;
value = '';
try {
value = moment(this[fieldName]).format(this.dateInputFormat);
value = moment(this[fieldName]).format(dateInputFormat);
} catch (err) {
value = '';
}
Expand All @@ -171,7 +180,10 @@ export class BaseResourceModel {
}
return value;
}
setDateInput(fieldName: string, value: any) {
setDateInput(fieldName: string, value: any, dateInputFormat?: string) {
if (dateInputFormat === undefined) {
dateInputFormat = this.dateInputFormat;
}
if (this[fieldName] === undefined) {
this[fieldName] = null;
return;
Expand Down

0 comments on commit 0c0b018

Please # to comment.