从datetime转换为时间并将其绑定到角度Timepicker?

时间:2015-03-30 20:27:22

标签: javascript json angularjs

我有角度Timepicker我想从json对象绑定日期但我的问题是错误

  

Timepicker指令:“ng-model”值必须是Date对象,一个数字   自1970年1月1日起的毫秒数或表示RFC2822的字符串   或ISO 8601 date.angular.js:11607(匿名函数)

user.until中的字符串=“2015-03-27T16:30:00” 我使用时刻js转换为时间我调试它输出12:30 pm

我想问一下如何将json中的时间绑定到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]);

1 个答案:

答案 0 :(得分:1)

我猜你传递了一个无效的String,尝试传递一个javascript Date对象。

var yourDate = "2015-03-27T16:30:00";
var timeInMilli = moment(yourDate, moment.ISO_8601).unix() * 1000;

$scope.dt = new Date(timeInMilli);

这个plunker说明了这个解决方案。