After searching for a solution, I came across a similar question that did not address the specific issue I am dealing with.
The challenge I am facing involves parsing a date into a string. Below is my current implementation:
export class DataFormater {
formatDDMMMYYY(date: Date): string {
return date.getDay().toString() + '/' + date.getMonth().toString() + '/' + date.getFullYear().toString();
}
}
However, an error occurs stating that date.getDay() is not a function
. Upon reviewing the documentation, I discovered that it is returning a number, hence the need to use .toString()
. Has anyone encountered this issue before or come up with a better way to parse a date into a desired output string?
-------------- Full Implementation ----------
<input class="form-control" placeholder="yyyy-mm-dd"
name="dp" ngbDatepicker #d="ngbDatepicker" [(ngModel)]="fromDate" (keyup)="setFilter()">
In Component
toDate: Date;
fromDate: Date;
constructor(private dateFormatter: DataFormater) {
this.toDate = new Date();
this.fromDate = new Date();
}
setFilter() {
if (this.toDate != null) {
console.log(this.toDate + " in toDate...");
this.filter.toDate = this.dateFormatter.formatDDMMMYYY(this.toDate);
}
if (this.fromDate != null) {
this.filter.fromDate = this.dateFormatter.formatDDMMMYYY(this.fromDate);
}
this.filterEmitter.emit(this.filter);
}
DataFormater Class
export class DataFormater {
constructor(){
console.log(this.formatDDMMMYYY(new Date))
}
formatDDMMMYYY(date: Date): string {
date = new Date(date);
return date.getDay().toString() + '/' + date.getMonth().toString() + '/' + date.getFullYear().toString();
}
}