0

I need to put a default value in my input datetime-local but for some reason it is not populated in angular. Please tell me what I need to do to make this right. My code is below

<!--Completion Date-->
<div class="form-group">
    <input
      formControlName="completionDate"
      type="datetime-local"
      id="completionDate"
      placeholder="yyyy-MM-ddTHH:mm:ss"
      [min]="date"
      class="form-control"
      required
    />
</div>

populateDate(): string {
    this.dateObj = new Date();
    this.month = this.dateObj.getUTCMonth() + 1;
    this.day = this.dateObj.getUTCDate();
    this.year = this.dateObj.getUTCFullYear();

    return `${this.year}-${this.month}-${this.day}`;
}

createTroubleTicketForm() {
    this.troublTicketForm = this.fb.group({
      ttNum: [null, Validators.required],
      status: [null, Validators.required],
      completionDate: [null],
      incident: [null, Validators.required],
      rootCause: [null, Validators.required],
      resolution: [null],
      category: [null, Validators.required],
      plazaCode: [null, Validators.required],
      laneNum: [null, Validators.required],
      reportedBy: [null],
      helpDeskAgent: [null, Validators.required],
      assignedToTier: [null],
      ticketCreated: [this.populateDate(), Validators.required],
      remarks: [null]
    });
}

1 Answer 1

4

You'd need to convert date into ISO format before it could be assigned via [ngModel];

relevant TS:

  completeDate: Date;
  localCompleteDate: string;
  constructor() {
    this.completeDate = new Date();
    this.localCompleteDate = this.completeDate.toISOString();
    this.localCompleteDate = this.localCompleteDate.substring(0, this.localCompleteDate.length - 1);
  }

relevant HTML:

<input
  type="datetime-local"
  id="completionDate"
  [ngModel] = "localCompleteDate"
  placeholder="yyyy-MM-ddTHH:mm:ss"
  [min]="date"
  class="form-control"
  required
/>
<br/>
Date: {{completeDate | date:'short'}}
<br/>
ISO Date: {{ localCompleteDate }}

check complete working stackblitz here

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

6 Comments

can you share the angular version? did you remove the last 'Z' character from the converted ISO date?
I am using angular 8. I copy pasted your code. where is that last 'Z' character?
this.localCompleteDate = this.localCompleteDate.substring(0, this.localCompleteDate.length - 1); has a substring function where I am removing the last character... If you remove this line, you would see the date in ISO format with character Z as the last character of that date in ISO format
I see.. I really can't figure out why it works on your code and not mine...
on the HTML output... do you see any difference between my SB and your program?
|

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.