QWERTY
QWERTY

Reputation: 2315

Pre-setting today date to input type date in Angular

I was having some problem when trying to preset today's date to HTML component using Angular Typescript. Here is my HTML component:

<div class="d-flex">
    <input id="field_tStartdate" type="date" class="form-control" name="tStartdate" [(ngModel)]="tStartdate" required />
</div>

Then in my typescript file:

tStartdate: string;
this.tStartdate = moment().toString();

However, my input field at the front end is still showing dd/MM/yyyy by default without pre-selecting today's date. Any ideas?

Thanks!

Upvotes: 0

Views: 1411

Answers (2)

DerStoffel
DerStoffel

Reputation: 2633

Input type date expects a date string with the format:

'YYYY-MM-DD'

For example in moment js this looks like that:

moment().format('YYYY-MM-DD')

Please be aware that upper and lower case matters, since 'mm' is Minutes while 'MM' refers to months.

Upvotes: 0

Bernard Pagoaga
Bernard Pagoaga

Reputation: 1396

The input type date expects a value of type "YYYY-mm-DD", but moment().toString() ouputs "Fri Apr 05 2019 17:25:24 GMT+0200"

Try moment().format('YYYY-mm-DD')

Upvotes: 1

Related Questions