Reputation: 3340
I was wondering is there a straight forward way to get the time in the ng-bootstrap datepicker? So far my code is:
<input class="form-input" type="text" id="date" placeholder="yyyy-mm-dd hh:mm:ss" name="dp" [(ngModel)]="model" ngbDatepicker #d="ngbDatepicker" [navigation]="navigation"
formControlName="date" (click)="d.toggle()"><i class="form-icon icon icon-calendar calendar-icon" (click)="d.toggle()"></i><h6 class="text-gray mt-2">xxxxx</h6>
ts file:
model: NgbDateStruct;
date: {year: number, month: number};
Any ideas?
Upvotes: 0
Views: 20939
Reputation: 21
You can use this
https://www.npmjs.com/package/ng-bootstrap-datetime-angular
https://stackblitz.com/edit/ng-bootstrap-datetime-angular
Install with npm:npm install ng-bootstrap-datetime-angular --save
Add NgBootstrapDateTimeAngularModule to your @NgModule like example below
import { BrowserModule } from "@angular/platform-browser";
import { NgModule } from "@angular/core";
import { AppComponent } from "./app.component";
import { FormsModule, ReactiveFormsModule } from "@angular/forms";
import { CommonModule } from "@angular/common";
import { NgBootstrapDateTimeAngularModule } from "ng-bootstrap-datetime-angular";
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
FormsModule,
ReactiveFormsModule,
NgBootstrapDateTimeAngularModule,
CommonModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {}
Add your input into form like example below
<form>
<div [formGroup]="formGroup">
<ng-bootstrap-datetime-angular
inputDatetimeFormat="dd/MM/yyyy hh:mm a"
formControlName="activeEndDate"
name="activeEndDate"
></ng-bootstrap-datetime-angular>
</div>
</form>
Connect to your component
import { Component, OnInit } from "@angular/core";
import { FormGroup, FormControl, Validators } from "@angular/forms";
@Component({
selector: "app-root",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.scss"]
})
export class AppComponent implements OnInit {
title = "Date";
formGroup: FormGroup;
ngOnInit() {
this.formGroup = new FormGroup({
activeEndDate: new FormControl(null, {
validators: [Validators.required]
})
});
}
}
Upvotes: 1
Reputation: 42556
I hope I am not too late, but since you are already using Ng Bootstrap, you might as well use the TimePicker module which is part of Ng Bootstrap. This way, you can simply import it together with the DatePicker module.
import { NgbDatepickerModule, NgbTimepickerModule } from '@ng-bootstrap/ng-bootstrap';
I have previously made a stackblitz demo as someone else asked a similar question. Feel free to check it out and see how it works!
Upvotes: 1
Reputation: 177
You have to use Timepicker of ngx-bootstrap. If you are really in need to implement time and date in same pop-up, then try the link below.
https://www.npmjs.com/package/ngx-bootstrap-datetime-popup
Upvotes: 1