gpt4 book ai didi

javascript - 如何在 Angular 2中以YYYY-MM-DD格式获取日期

转载 作者:太空狗 更新时间:2023-10-29 18:05:13 25 4
gpt4 key购买 nike

<分区>

我想获取以下格式的日期: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;
}
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com