I had the same issue. Here is what I did using Jquery-UI calendar with Angularjs
date format was "2015-03-24T04:00:00"
First trim the date string to get only year, month, and date.
var date = "2015-03-24T04:00:00"
var formattedDate = date.match(/[\d-]+/).pop();
// formattedDate is now "2015-03-24" which is passed into
// the directive below as the input.$modelValue.
Now, inside your directive or controller do the following...
// Here is directive example.
link: function( scope, element, attrs, input ){
element.datepicker( optionsObjectHere );
setInitialDateFormatOnInput();
function setInitialDateFormatOnInput(){
setTimeout(function(){ // This timeout is required to delay the directive for the input.modelValue to resolve, however, no actual delay occurs!
element.datepicker( "setDate", formatToJqueryUIDateFormat());
});
}
function formatToJqueryUIDateFormat(){
return $.datepicker.parseDate( 'yy-mm-dd', input.$modelValue );
// 'yy-mm-dd' needs to match the input.$modelValue format from above.
}
} // link
This is how I use the entire jquery UI on my inputs.
HTML
<input type="text" class="inline" ng-model="inputValue" my-calendar-popup="calendarOptions" />
Where calendarOptions is the following object
var calendarOptions = { minDate: 0, buttonImage: "calendar-icon.png", buttonImageOnly: 'true', showOn: "both", dateFormat: "MM d, yy" };
DIRECTIVE
app.directive('myCalendarPopup', function(){
var defaultOptions = { minDate: 0, buttonImage: "calendar-icon.png", buttonImageOnly: 'true', showOn: "both", dateFormat: "MM d, yy" };
// defaultOptions just in case someone doesn't pass in options.
return {
require:'?ngModel',
restrict: 'A',
link: function( scope, element, attrs, input ){
if ( !input ){ return; } // If no ngModel then return;
element.datepicker( createCalendarOptions());
setInitialDateFormatOnInput();
function createCalendarOptions(){
if( !attrs.rsCalendarPopup ){ return addRequiredJqueryFunction( defaultOptions );}
return formatOptions();
}
function formatOptions() {
var options = scope.$eval( attrs.rsCalendarPopup );
// Turn string into object above.
return addRequiredJqueryFunction( options );
}
function addRequiredJqueryFunction( options ){
options.onSelect = changeDate;
// add onSelect to passed in object and reference local changeDate function, which will update changes to input.$modelValue.
return options;
}
function changeDate( date ){
input.$setViewValue( date );
}
function setInitialDateFormatOnInput(){
setTimeout(function(){
// This timeout is required to delay the directive for the input.modelValue to resolve.
// However, there is no actual timeout time. This is a must to get
// Angular to behave.
element.datepicker( "setDate", formatToJqueryUIDateFormat());
});
}
function formatToJqueryUIDateFormat(){
return $.datepicker.parseDate( 'yy-mm-dd', input.$modelValue );
// 'yy-mm-dd' is not the format you want the calendar to be
// it is the format the original date shows up as.
// you set your actual formatting using the calendar options at
// the top of this directive or inside the passed in options.
// The key is called dateFormat, in this case it's set as
// dateFormat: "MM d, yy" which makes June 30, 2015.
}
} // link
} // return
});
Note: I can only get this to work in it's displayed configuration. I have added it to a controller, and even a directive controller and could not recreate the initial date condition. I have not found out why this is yet. Perhaps this solution works only within a link function that is embedded in another isolated scope directive and works due to specific timing.