我有 Angular 时间选择器,我想从 json 对象绑定(bind)日期,但我的问题是错误
Timepicker directive: "ng-model" value must be a Date object, a number of milliseconds since 01.01.1970 or a string representing an RFC2822 or ISO 8601 date.angular.js:11607 (anonymous function)
user.until = "2015-03-27T16:30:00"中的字符串 我使用 moment js 转换为我调试的时间,它正在输出 12:30 pm
只是想问一下如何将json中的时间绑定(bind)到Timepicker。我不明白我正在转换为日期,但 Timepicker 提示它不是日期对象?
var app = angular.module('userUpdate');
EditUserController = function($scope, $modalInstance,user){
$scope.model = {
personID: user.personID,
until:user.until,
status: 0,
message: user.message
}
$scope.dt = moment.utc(user.until).local().format('MM/DD/YYYY');
$scope.tm = moment.utc(user.until).local().format('hh:mm a');
$scope.defaultStatus = '1';
$scope.clear = function(){
$scope.dt = null;
};
$scope.open = function($event){
$event.preventDefault();
$event.stopPropagation();
$scope.opened = true;
};
$scope.dateOptions = {
formatYear: 'yy',
startingDay: 1
};
//Timepicker Settings
//$scope.time = new Date();
//$scope.currenTime = function () {
// var time = new Date();
// var h = time.getHours();
// var m = time.getMinutes();
// if (m < 10) {
// m = '0' + m;
// }
// time = h + ':' + m;
// $scope.tm = time;
//}
$scope.hstep = 1;
$scope.mstep = 15;
$scope.ismeridian = true;
$scope.toggleMode = function(){
$scope.ismeridian = !$scope.ismeridian;
};
var formatDateTimeData = function (date, time) {
var until = '';
var dd = date.getDate();
var mm = date.getMonth() + 1; //January is 0!
var yyyy = date.getFullYear();
var h = time.getHours();
var m = time.getMinutes();
if (dd < 10) {
dd = '0' + dd;
}
if (mm < 10) {
mm = '0' + mm;
}
if (m < 10) {
m = '0' + m;
}
until = mm + '/' + dd + '/' + yyyy + ' ' + h + ':' + m;
return until;
}
$scope.ok = function (model){
var untilTemp = formatDateTimeData($scope.dt, $scope.tm);
$modalInstance.close({
personID: model.personID,
until: untilTemp,
status: model.status,
message: model.message
});
};
$scope.cancel = function(){
$modalInstance.dismiss('cancel');
};
};
app.controller('EditUserController', ['$scope', '$modalInstance', EditUserController]);
最佳答案
我猜你传递了一个无效的字符串,尝试传递一个 javascript 日期对象。
var yourDate = "2015-03-27T16:30:00";
var timeInMilli = moment(yourDate, moment.ISO_8601).unix() * 1000;
$scope.dt = new Date(timeInMilli);
这plunker说明这个解决方案。
关于javascript - 从日期时间转换为时间并将其绑定(bind)到 Angular 时间选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29355654/