如何将日期转换为字符串格式年-月-日在打字稿中
现在我得到的日期为2017 年 3 月 24 日星期五 00:00:00 GMT-0400(东部夏令时间)
我只需要格式中的日期"2017-03-24"从此并且没有任何时区和时区转换
假设您使用 Angular,您可以将 DatePipe 或 FormatDate 导入到您的 component.ts 中来处理该问题。您可以阅读更多有关各种日期/时间格式您可以作为参数传递。
1)日期管道:
import { DatePipe } from '@angular/common';
export class SampleComponent implements OnInit {
constructor(private datePipe: DatePipe) {}
transformDate(date) {
return this.datePipe.transform(date, 'yyyy-MM-dd');
}
}
不要忘记将 DatePipe 添加到模块中的提供程序中。
providers: [DatePipe]
2)格式日期:
import { formatDate } from '@angular/common';
export class SampleComponent implements OnInit {
constructor(@Inject(LOCALE_ID) private locale: string) {}
transformDate(date) {
return formatDate(date, 'yyyy-MM-dd', this.locale);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)