I want to get date in the following format: YYYY-MM-DD.
I wrote a date service in Angular2 based on this question: How do I get the current date in JavaScript?.
I wanted to check if it is a correct implementation or if there exists a better way to achieve the goal.
import { Injectable } from '@angular/core';
@Injectable()
export class DateService {
private currentDate = new Date();
getCurrentDateInApiFormat(): string{
let day:any = this.currentDate.getDate();
let month:any = this.currentDate.getMonth() + 1;
let year:any = this.currentDate.getFullYear();
let dateInApiFormat: string;
if(day<10){
day = '0' + day.toString();
}
if(month<10){
month = '0' + month.toString();
}
dateInApiFormat = day + '-' + month + '-' + year.toString();
return dateInApiFormat;
}
}