I have spent sometime on researching the existing datetime directives of angularjs.
Both ngularUI and AngularStrap do not provide a datetimepicker as I needed. Of course, I know use a datepicker and timepicker together to archive the purpose.
I have searched the related topic from internet and stackoverflow. Found some interesting and helpful info.
http://dalelotts.github.io/angular-bootstrap-datetimepicker/, there is a datetimepicker, but I dislike the usage of this directive.
connecting datetimepicker to angularjs , this topic is very helpful, I tried to wrap my datetimepicker directive following the steps.
My work is based on https://github.com/Eonasdan/bootstrap-datetimepicker, a bootstrap 3 based datetimepicker, the UI is very nice.
app.directive('datetimepicker', function() {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, element, attrs, ngModelCtrl) {
console.log('call datetimepicker link...');
var picker = element.datetimepicker({
dateFormat: 'dd/MM/yyyy hh:mm:ss'
});
//ngModelCtrl.$setViewValue(picker.getDate());
//model->view
ngModelCtrl.$render(function() {
console.log('ngModelCtrl.$viewValue@'+ngModelCtrl.$viewValue);
picker.setDate(ngModelCtrl.$viewValue || '');
});
//view->model
picker.on('dp.change', function(e) {
console.log('dp.change'+e.date);
scope.$apply(function(){
ngModelCtrl.$setViewValue(e.date);
});
});
}
};
});
And use it in my view.
<div class="col-md-6">
<div class="input-group date" id="endTime" data-datetimepicker ng-model="stat.endTime" data-date-format="MM/DD/YYYY hh:mm A/PM" >
<input class="form-control" type="text" placeholder="End"/>
<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
There are some problems I found.
Thanks in advance.
The JavaScript DateTime Picker is a lightweight and mobile-friendly control that allows end users to enter or select date and time values from a pop-up calendar and drop-down time list. It provides month, year, and decade views for quick navigation to the desired date.
The md-datepicker, an Angular Directive, is an input control to select a date and supports ngMessages for input validation.
I had the same issue. Here's what I ended up doing that worked well for me:
'use strict';
angular.module('frontStreetApp.directives')
.directive('psDatetimePicker', function (moment) {
var format = 'MM/DD/YYYY hh:mm A';
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, element, attributes, ctrl) {
element.datetimepicker({
format: format
});
var picker = element.data("DateTimePicker");
ctrl.$formatters.push(function (value) {
var date = moment(value);
if (date.isValid()) {
return date.format(format);
}
return '';
});
element.on('change', function (event) {
scope.$apply(function() {
var date = picker.getDate();
ctrl.$setViewValue(date.valueOf());
});
});
}
};
});
Here's the HTML:
<!-- The dueDate field is a UNIX offset of the date -->
<input type="text"
ng-model="dueDate"
ps-datetime-picker
class="form-control">
You can check out the gists and a bit more information in my blog post.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With