Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

angular - 在角度组件中以html显示当前日期(Show current date in html in angular component)

I use angular 8. In my html I need to display current date.

(我使用angular8。在我的html中,我需要显示当前日期。)

Here is html code:

(这是html代码:)

   <label class="col-md-4">Creation date </label>
   <input type="date" class="form-control" formControlName="date"/>

How can I display in html above current date?

(如何在当前日期之上的html中显示?)

  ask by Michael translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your component.ts file, you can get the date as,

(在您的component.ts文件中,您可以获得日期,)

export class AppComponent implements OnInit {
   dateVal = new Date();

and display in the component.html, if you want to format you can use DatePipe as you need

(并显示在component.html中,如果要格式化,可以根据需要使用DatePipe)

{{dateVal | date: 'M/dd/yyyy'}}

If you need to display in input, you can do

(如果需要在输入中显示,可以执行)

<input type="date" [ngModel] ="dt | date:'yyyy-MM-dd'" (ngModelChange)="dt = $event">

DEMO

(演示)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...