title: how to get date in YYYY-MM-DD format in angular 2
body: <p>I want to get date in the following format: YYYY-MM-DD.</p>
  
  <p>I wrote a date service in Angular2 based on this question: 
  <a href="https://stackoverflow.com/questions/1531093/how-do-i-get-the-current-date-in-javascript">How do I get the current date in JavaScript?</a>. </p>
  
  <p>I wanted to check if it is a correct implementation or if there exists a better way to achieve the goal.</p>
  
  <pre class="lang-js prettyprint-override"><code>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&lt;10){
             day = '0' + day.toString();
          }
          if(month&lt;10){
              month = '0' + month.toString();
          }
          dateInApiFormat = day + '-' + month + '-' + year.toString();
          return dateInApiFormat;
      }
  }
  </code></pre>