<分区>
我想获取以下格式的日期:YYYY-MM-DD。
我根据这个问题在 Angular2 中写了一个日期服务: How do I get the current date in JavaScript? .
我想检查它是否是正确的实现,或者是否存在实现目标的更好方法。
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;
}
}