32

I have tried many date picker in my angular2 app but none of them is working.Although the date picker is displaying on the view but the value of selected date is not getting in the ngModel variable.

Mubashir
  • 4,134
  • 4
  • 22
  • 30
  • 1
    I don't think date picker has to do anything with it. Because no data binding is required in angular 2 – binariedMe Jan 14 '16 at 11:53
  • May be you post the code with corresponding datepicker library. – binariedMe Jan 14 '16 at 11:53
  • Here is a working solution with the jquery datepicker: http://stackoverflow.com/questions/36087762/how-to-detect-bootstrap-datetimepicker-change-events-within-angular2 – JSM Apr 21 '16 at 09:45
  • Here is a solution with the Jquery DatePicker http://stackoverflow.com/questions/36087762/how-to-detect-bootstrap-datetimepicker-change-events-within-angular2 – JSM Apr 21 '16 at 09:49

10 Answers10

52

In fact, you can use a datepicker by simply adding the date value into the type attribute of your inputs:

<input type="date" [(ngModel)]="company.birthdate"/>

In some browsers like Chrome and Microsoft Edge (not in Firefox), you can click on the icon within the input to display the date picker. Icons appear only when you mouse is over the input.

To have something cross browsers, you should consider to use Angular2 compliant libraries like:

Matthew Pitts
  • 809
  • 1
  • 8
  • 13
Thierry Templier
  • 198,364
  • 44
  • 396
  • 360
5

PrimeNG also provides a DatePicker component, live demo;

http://www.primefaces.org/primeng/#/calendar

Cagatay Civici
  • 6,406
  • 1
  • 29
  • 34
  • if i run this date picker in `--prod` mode of angular throws error of 'jQuery is not defined` ? why so ? – Pardeep Jain Jul 22 '16 at 10:54
  • 1
    the only problem I have with this one is that it doesn't bind to a Date object. it uses strings :( – Rafe Jul 28 '16 at 22:19
  • You need to define dependencies on `JQuery`, `JQuery UI` and `JQuery UI TimePicker AdOn`. See answer at http://stackoverflow.com/a/38255111/641103 – A J Qarshi Aug 04 '16 at 10:39
2

As answered by @thierry yes there is option for us to use

<input type="date" [(ngModel)]="company.birthdate"/>

for getting date for our project. but yes this is not compatible for the multi browser plateform (like mozila) and there are many cross browser library for the same.

I have made two stylish calendar Datepickers using Bootflat theme which is responsive too refer here

https://github.com/MrPardeep/Angular2-DatePicker

enter image description here

hope this gives you more stylish and multi browser datepicker.

Pardeep Jain
  • 84,110
  • 37
  • 165
  • 215
1

Not sure if it helps but we wanted to develop a custom datepicker for our project and we couldn't find a lot of Angular2 Datepicker's at that point and hence ended up writing a simple one myself.

Its a simple one which selects the date and you can also specify dates that you want to disable before and after. It uses event emitter and sets the selected date in a text field. It is published in npm and I am working on improving it as well.

https://www.npmjs.com/package/angular2-simple-datepicker

Hope it helps.

Bhuvana L
  • 21
  • 3
1

Angular 2 (Typescript)

can also do this with blur event.

look closely at (blur)="newProjectModel.eEDate = eEDatePicker.value" on blur event it assigns input temporary variable #eEDatePicker value to our model variable newProjectModel.eEDate

<form (ngSubmit)="onSubmit()" #newProjectForm="ngForm" >
  <div class="row">
    <div class="col-sm-4">
      <div class="form-group">
        <label for="end-date">Expected Finish Date</label>
        <div class='input-group date datetimepicker'>
          <input type='text' class="form-control" required [(ngModel)]="newProjectModel.eEDate" name="eEDate" #eEDate="ngModel" #eEDatePicker (blur)="newProjectModel.eEDate = eEDatePicker.value" />
          <span class="input-group-addon">
            <span class="fa fa-calendar"></span>
          </span>
        </div>
        <div [hidden]="eEDate.valid || eEDate.pristine" class="alert alert-danger">
          Expected Finish Date is required
        </div>
      </div>
    </div>
  </div>
  <div class="modal-footer" [hidden]="submitted">
    <button [hidden]="submitted" type="button" class="btn btn-red" data-dismiss="modal" >Cancel</button>
    <button [hidden]="submitted" type="submit" class="btn btn-green" [disabled]="!newProjectForm.form.valid">Save</button>
  </div>
  <div class="modal-footer" [hidden]="!submitted">
    <button [hidden]="!submitted" focus class="btn btn-green" data-dismiss="modal"  (click)="submitted=false">Ok</button>
  </div>
</form>

I'am using these bootstrap libraries:

bootstrap-datetimepicker.min.css

bootstrap-datetimepicker.min.js

Kaleem Ullah
  • 6,799
  • 3
  • 42
  • 47
1

consider using angular-datepicker, it's a highly configurable date picker built for Angular applications.

enter image description here

vlio20
  • 8,955
  • 18
  • 95
  • 180
  • 1
    For me this is the best answer at this moment. The other two from accepted answer won't do the trick in my case - mydatepicker has overcomplicated data model unsuitable to bind nicely in reactive form if you want simple date string on output, ng2-datepicker - this won't even start with newest Angular (9.1.2). – hpopiolkiewicz Apr 24 '20 at 12:04
0

See Stackoverflow question, ng2-boostrap datePicker seems to overwrite ngModel, while the question is different, the answers given to it solved the issue you described for me.

Assuming using typescript, you need to add this to the html markup:

<datepicker [(ngModel)]="dateValue" [showWeeks]="false"</datepicker>

and in the component ts file, you will need to import

import {DATEPICKER_DIRECTIVES} from 'ng2-bootstrap/ng2-bootstrap';

and add the DATEPICKER_DIRECTIVES to your providers list.

[Edit: This applies to an older version of Angular 2. There are much better choices in current versions]

Herb F
  • 135
  • 8
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Uyghur Lives Matter Jun 03 '16 at 18:42
  • 2
    I'm new to posting here, but why did you both post the exact same complaint about the linked page at stackoverflow? I can easily make the changes suggested. – Herb F Jun 03 '16 at 19:40
  • outdated outdated – Toolkit Dec 26 '16 at 20:36
0

This is my solution:

HTML

<input id="send_date"
       type="text"
       [(ngModel)]="this.dataModel.send_date"
       name="send_date"
       #send_date="ngModel"
       class="date-picker"
       data-date-format="dd-mm-yyyy">

.TS

//My data model has "send_date" property
dataModel: MyDataModel;

ngAfterViewInit() {

    //important point: You have to create a reference to this outer scope
    var that = this;

    $('#send_date').datepicker({
        //... your datepicker attributes
    }).change(function () {
        that.dataModel.send_date = $('#send_date').val();
    });

}
ali6p
  • 1,683
  • 13
  • 17
0

I just found mydatepicker package today just type in npm install mydatepicker --save and follow the instructions here if you don't like their naming style, just create a wrapper component around it that works with Date type

shakram02
  • 10,812
  • 4
  • 22
  • 21
0

I am not sure if its gonna work on older versions of angular because i made it in Angular 13.1.0 version but you can give it a shot its very simple to use.

npm i @handylib/ngx-datepicker

then import NgxDatepickerModule to your module

there are 5 directives which you can use according to your needs

  1. datetimepicker for date nad time
  2. datepicker for date only
  3. timepicker for time only
  4. monthpicker for month only
  5. yearpicker for year only

USAGE

<input type="text" datetimepicker placeholder="Please select date and time">

To use custom format

<input type="text" datetimepicker [format]="'DD MMMM, YYYY hh:m A'" placeholder="Please select date and time">

i am still updating its documents but i think for angular 13+ its very simple to impliment as i have already used it on whole lot of projects.

Material Look

Vikas Kandari
  • 1,612
  • 18
  • 23