Ngx-bootstrap: feat(datepicker): add DateTimePicker

Created on 27 May 2016  路  18Comments  路  Source: valor-software/ngx-bootstrap

Something similar to https://github.com/Eonasdan/bootstrap-datetimepicker would be immensely useful. I'm not sure if it's an edge case, but I'd love for this to be doable at least. :)

comp(datepicker) enhancement

Most helpful comment

any update on this feature ?

All 18 comments

@yusijs I just implemented something similar. It's very basic and it makes use of the ng2-bootstrap datepicker and timepicker.

I don't have this in any commit, but here is the code :

import { Component, OnInit, Input, Self } from '@angular/core';
import { NgModel, ControlValueAccessor } from '@angular/common';
import { TimepickerComponent } from 'ng2-bootstrap';
import { DATEPICKER_DIRECTIVES } from 'ng2-bootstrap/ng2-bootstrap';


@Component({
    selector: 'date-picker',
    template: require('./date-picker.component.html'),
    styles: [require('./date-picker.component.scss')],
    directives: [DATEPICKER_DIRECTIVES, TimepickerComponent]
})

export class DateTimePickerComponent implements ControlValueAccessor, OnInit {

    @Input() milliseconds: boolean;
    public cd: NgModel;
    public onChange: any = Function.prototype;
    public onTouched: any = Function.prototype;
    internalDate: Date;
    internalTime: Date;
    internalDateTime: Date;

    public constructor(@Self() cd: NgModel) {

        this.cd = cd;
        cd.valueAccessor = this;
    }


    ngOnInit() {

        this.internalDate = this.internalDateTime;
        this.internalTime = this.internalDateTime;
    }


    writeValue(obj: any) : void {

        if (obj === this.internalDateTime) {
            return;
        }
        if (obj && obj instanceof Date) {
            this.internalDateTime = obj;
            return;
        }
        this.internalDateTime = obj ? new Date(obj) : void 0;
    }


    registerOnChange(fn: any) : void {

        this.onChange = fn;
    }


    registerOnTouched(fn: any) : void {

        this.onTouched = fn;
    }


    updateDateTime() {

        let newDate: Date = new Date();
        newDate.setTime(this.internalDate.getTime());
        newDate.setHours(this.internalTime.getHours());
        newDate.setMinutes(this.internalTime.getMinutes());
        newDate.setSeconds(this.internalTime.getSeconds());
        newDate.setMilliseconds(this.internalTime.getMilliseconds());
        this.internalDateTime = newDate;
        if (this.milliseconds) {
            this.cd.viewToModelUpdate(newDate.getTime());
        }
        else {
            this.cd.viewToModelUpdate(newDate);
        }
    }


    get date(): Date {

        return this.internalDate;
    }

    set date(date: Date) {

        this.internalDate = date;
        this.updateDateTime();
    }


    get time(): Date {

        return this.internalTime;
    }

    set time(time: Date) {

        this.internalTime = time;
        this.updateDateTime();
    }
}

and template looks like this :

<div class="row">
    <div class="col-xs-12">
        <datepicker [(ngModel)]="date" [minDate]="minDate" [showWeeks]="false"></datepicker>
        <timepicker [(ngModel)]="time" [hourStep]="1" [minuteStep]="15" [showMeridian]="false"></timepicker>
    </div>
</div>

Usage is done so :

<date-picker *ngIf="isEditable" [(ngModel)]="flyer.startDate" [milliseconds]="true"></date-picker>

You could then accept all inputs from both components and pass them along as needed, styling it the way you like, etc. For the project I am working on this was sufficient and there was no need to do anything more.

Hope this helps!

Thanks - I'll give it a try a bit later today! Do you have the .scss file as well? I'm terrible with styles :3

@yusijs This is the contents of that file :


As you can see... I am also bad with styling.

@jppellerin It's like we're soulmates. :+1:

I started putting something a bit more official together - hopefully to lead towards a pull request soon-ish. Pretty busy of late, so I cannot predict when I will be finished.

@jppellerin if you have this on a git repo, I could assist?

Thanks for issue, I was thinkin
Should I include time picker in datepicker or not :)
thanks for an issue! :)

Would be immensely useful. I've made a silly hack now where I bind them to hidden form values, them merge them later in my code. Feels dirty. :p

I see that there is a branch for this, what's the status?
Is help needed/useful?

any update on this feature ?

any updates on this feature?

Any updates?

Any updates on this feature ?

It's been more than 2 years since this feature request. Any type of ETA when it will be included?

I wouldn't mind if there was a dirty hack for this now
But it looks like I'm going to have to quickly write my own dirty hack ...

Still no updates?

Almost 4 years later... Any updates?
Would be worth noting this is the 5th oldest open issue of this library. ^.^

Any updates on this? I see there are 2 external libraries... Perhaps one can be leveraged?

https://github.com/gillardo/ngx-bootstrap-datetime-popup
https://github.com/Eonasdan/bootstrap-datetimepicker

Was this page helpful?
0 / 5 - 0 ratings

Related issues

phmello picture phmello  路  3Comments

tpiros picture tpiros  路  3Comments

ctrl-brk picture ctrl-brk  路  3Comments

KimBum picture KimBum  路  3Comments

webdev48 picture webdev48  路  3Comments