3

I use the below to generate date in component.ts file:

this.myDate=new Date();

And try to use in html form value field.But nothing is loaded.I have tried both the below syntax but not succesful

<input type="datetime" class="form-control" id="date" value={{this.myDate}} name="created" ngModel>

<input type="datetime" class="form-control" id="date" [value]=[this.myDate] name="created" ngModel>

One more issue is that my json file is upadted with only empty string after add operation

2 Answers 2

1

You are missing the right date format for the input type datetime that is ="2017-06-01T08:30"

in the component.ts

formatted_date:string;

ngOnInit(){
//your code...
this.formatted_date = this.myDate.toISOString();
this.formatted_date.slice(0,16);
}

In the template.html (change the type to datetime-local beacuse datetime is Obsolete MDN Doc)

<input type="datetime-local" class="form-control" id="date" value="{{formatted_date}}" name="created">
Sign up to request clarification or add additional context in comments.

Comments

0

It works if you remove the ngModel directive (see this stackblitz):

<input type="datetime" class="form-control" id="date" value="{{myDate}}" name="created">
<input type="datetime" class="form-control" id="date" [value]="myDate" name="created">

An alternative is to set the value with [ngModel] (see this stackblitz):

<input type="datetime" class="form-control" id="date" [ngModel]="myDate" name="created">

3 Comments

I get only mm/did/yy in date field
Check if the data is correct with this.myDate = new Date(); console.log(this.myDate).
I get date and time in console

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.