user474901
user474901

Reputation:

convert from datetime to time and bind it to angular Timepicker?

I have angular Timepicker I want to bind date from json object but my problem this is error

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)

the string in user.until = "2015-03-27T16:30:00" I have using moment js to convert to time I debug it is outputing 12:30 pm

just I want to ask how can I bind time in json to Timepicker. I do't get it I' converting to date but Timepicker is complaining about it is not date object ?

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]);

Upvotes: 4

Views: 1567

Answers (1)

Rigotti
Rigotti

Reputation: 2862

I guess you're passing an invalid String, try passing a javascript Date object.

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

$scope.dt = new Date(timeInMilli);

This plunker illustrate this solution.

Upvotes: 1

Related Questions