【问题标题】:convert from datetime to time and bind it to angular Timepicker?从日期时间转换为时间并将其绑定到角度时间选择器?
【发布时间】:2015-03-30 20:27:22
【问题描述】:

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

Timepicker 指令:“ng-model”值必须是 Date 对象、数字 自 01.01.1970 以来的毫秒数或表示 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]);

【问题讨论】:

    标签: javascript json angularjs


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-20
      • 1970-01-01
      • 2018-11-23
      • 2020-02-17
      • 2014-08-13
      • 2020-04-23
      相关资源
      最近更新 更多