question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Datepicker: can't set initual value using input & form control name

See original GitHub issue

I can’t set the initial value of a datepicker that is bound to an input. Sample code:

//.html
<form [formGroup]="form" class="form-default">
	<input type="text" class="form-control" formControlName="publishdate" ngbDatepicker #datepicker="ngbDatepicker">
</form>
<div class="input-group-addon" (click)="datepicker.toggle()"></div>
// ts
this.content.publishdate = new Date();
this.form.patchValue(this.content);

The input is not filled with the value from ‘content.publishdate’ and the datepicker when opened (through datepicker.toggle()) does not have the value from today. Is this correct? I didn’t find anythin in the docs about this.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

17reactions
irvinleecommented, Aug 11, 2018

I encountered the same problem. I fixed it by making sure that I’m passing in integers instead of strings:

this.form.get('birthDate').setValue({
        year: parseInt(birthDate.format('YYYY'), 10),
        month: parseInt(birthDate.format('M'), 10),
        day: parseInt(birthDate.format('D'), 10)
      });
7reactions
pkozlowski-opensourcecommented, Aug 8, 2017

You need to specify publishdate as an object (structure) with the year, month and day properties. Specifying new Date() won’t work. See http://plnkr.co/edit/kj2TIP2b1DfTtWkuHFGA?p=preview and check https://ng-bootstrap.github.io/#/components/datepicker/examples for more examples.

Closing as a support request.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular Reactive Form Can't assign default date value to ...
I am trying to assigned a custom date to the angular app datepicker, but no date value is showing.
Read more >
Set Datepicker to null / empty throws error?
You need to set the default date using date picker input, not the mdb-datepicker component. Here is an example: HTML: <mdb-form-control> <input mdbInput ......
Read more >
<input type="date"> - HTML: HyperText Markup Language
The date is formatted according to ISO8601, described in Date strings format. You can set a default value for the input with a...
Read more >
Datepicker - Angular powered Bootstrap
By default the input is used as a target. Type: string | HTMLElement. Default value: - — initialized from NgbInputDatepickerConfig service.
Read more >
Form Datepicker | Components - BootstrapVue
If <b-form-datepicker> has a value set for the name prop, a hidden input will be created which will have its name attribute set...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found