0

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!

2 Answers 2

1

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')

Sign up to request clarification or add additional context in comments.

2 Comments

Nope, it does not work. The input is still showing 'dd/mm/yyyy' instead of today's date
@hyperfkcb did you see the working example? stackblitz.com/edit/angular-ayudts
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.