【发布时间】:2018-07-12 07:42:23
【问题描述】:
我如何获得具有特定格式“yyyy-MM-dd”的当前日期,例如我今天的结果是:“2018-07-12”,仅使用命令
myDate = new Date();
非常感谢
【问题讨论】:
-
您可以使用角度日期管道检查此angular.io/api/common/DatePipe
-
我需要类型脚本文件中的值
标签: angular
我如何获得具有特定格式“yyyy-MM-dd”的当前日期,例如我今天的结果是:“2018-07-12”,仅使用命令
myDate = new Date();
非常感谢
【问题讨论】:
标签: angular
您可以使用 DatePipe 在 Angular 中格式化日期。
如果你想在 ts 中格式化日期,那么你可以像这样在构造函数中将 DatePipe 作为服务注入
import { DatePipe } from '@angular/common';
@Component({
templateUrl: './name.component.html',
styleUrls: ['./name.component.scss'],
providers: [DatePipe]
})
myDate = new Date();
constructor(private datePipe: DatePipe){
this.myDate = this.datePipe.transform(this.myDate, 'yyyy-MM-dd');
}
如果你想在 html 文件中格式化,'Shortdate' 将返回 MM/DD/YY 类型的日期
{{myDate | date: 'shortDate' }}
从 Angular 6 开始,这也有效,
import {formatDate} from '@angular/common';
formatDate(new Date(), 'yyyy/MM/dd', 'en');
【讨论】:
你可以试试这个。
component.ts
currentDate = new Date();
component.html
{{currentDate | date:'yyyy-MM-dd'}}
【讨论】:
你可以使用date:'yyyy-MM-dd'管道
curDate=new Date();
<p>{{curDate | date:'yyyy-MM-dd'}}</p>
【讨论】:
如果你只想显示,那么你可以像这样在 HTML 中使用日期管道:
只是日期:
your date | date: 'dd MMM yyyy'
日期与时间:
your date | date: 'dd MMM yyyy hh:mm a'
如果您只想从 new Date() 中获取输入日期,那么:
你可以像这样在 ts 文件中使用 angular formatDate() -
currentDate = new Date();
const cValue = formatDate(currentDate, 'yyyy-MM-dd', 'en-US');
【讨论】:
示例如下:
function MethodName($scope)
{
$scope.date = new Date();
}
您可以在此处更改格式,我们有代码
<div ng-app ng-controller="MethodName">
My current date is {{date | date:'yyyy-MM-dd'}} .
</div>
希望对你有帮助。
【讨论】:
在控制器中,
var DateObj = new Date();
$scope.YourParam = DateObj.getFullYear() + '-' + ('0' + (DateObj.getMonth() + 1)).slice(-2) + '-' + ('0' + DateObj.getDate()).slice(-2);
【讨论】:
试试这个:
import * as moment from 'moment';
ngOnInit() {
this.date = moment().format("YYYY Do MMM");
}
【讨论】:
app.component.html
<div>
<h5 style="color:#ffffff;">{{myDate | date:'fullDate'}}</h5>
</div>
app.component.ts
export class AppComponent implements OnInit {
myDate = Date.now(); //date
【讨论】:
要在 Angular 中格式化日期,我们可以使用 Angular Datepipe。
例如,我们可以使用以下代码在我们的代码中获取格式化日期。
import { DatePipe } from '@angular/common';
@Component({
selector: 'app-name-class',
templateUrl: './name.component.html',
styleUrls: ['./name.component.scss']
})
export class NameComponent implements OnInit {
// define datepipe
datePipe: DatePipe = new DatePipe('en-US');
constructor(){}
// method to get formatted date
getformattedDate(){
var date = new Date();
var transformDate = this.datePipe.transform(date, 'yyyy-MM-dd');
return transformDate;
}
}
【讨论】:
你可以使用简单的 var dt = new Date().toISOString().slice(0, 10);
【讨论】:
使用以下内容:
new Date().toLocaleDateString();
然后根据您的要求更改应用程序的语言环境:
import { registerLocaleData } from '@angular/common';
import localeFr from '@angular/common/locales/fr';
registerLocaleData(localeFr);
//Provide the locale in any Module or Component by the LOCALE_ID token.
providers: [
{provide: LOCALE_ID, useValue: 'fr'}
]
【讨论】:
可以在 HTML 中使用格式属性。
【讨论】:
format 属性?你能提供一个文档链接吗?演示如何在此处应用它的代码示例?为什么这种方法可能比这个线程上接受的答案更受欢迎?