how to combine values from Datepicker & Timepicker in one variable

匆匆过客 提交于 2021-01-29 02:04:06

问题


I have angular App U I have Datepicker & Timepicker using angular-ui when user select date $Scope.dt and set time $Scope.mytime I am trying to combine in one $Scope.SelectedDateTime I get NaN. I don't know how to fix it

update when user select date $scope.dt value will be 'ok when user select date it will 'Thu Mar 05 2015 00:00:00 GMT-0500 (Eastern Standard Time)' and $scope.mytime value 'Thu Mar 05 2015 23:30:00 GMT-0500 (Eastern Standard Time)' how to combine them in one variable html

      <h4>Popup</h4>
        <div class="row">
            <div class="col-md-6">
                <p class="input-group">
                  <input type="text" class="form-control" datepicker-popup="{{format}}" ng-model="dt" />
                  <span class="input-group-btn">
                    <button type="button" class="btn btn-default" ng-click="open($event)"><i class="glyphicon glyphicon-calendar"></i></button>
                  </span>
                </p>
            </div>
        </div>
    <timepicker ng-model="mytime" ng-change="changed()" hour-step="hstep" minute-step="mstep" show-meridian="ismeridian"></timepicker>
<button type="button" class="btn btn-sm btn-info" ng-click="SelectedDateTime()">Today</button>

javascript

    $scope.today = function() {
      $scope.dt = new Date();
    };
    $scope.today();
    $Scope.mytime = new Date();
 $scope.changed = function () {
    $log.log('Time changed to: ' + $scope.mytime);
  };

  $scope.clear = function() {
    $scope.mytime = null;
  };
    $Scope.SelectedDateTime = function()
    {
      var SelectedDateTime = $scope.dt +''+$scope.mytime;
    }

回答1:


You can't concatenate or try to add date objects as if they are numbers.

What you are basically trying to do is

new Date() +" "+ new Date()

Paste that line into browser console and you will see something like:

"Thu Mar 05 2015 21:54:37 GMT-0500 (Eastern Standard Time) Thu Mar 05 2015 21:54:37 GMT-0500 (Eastern Standard Time)"

You need to deal with them as Date objects and use appropriate Date prototype methods to manipulate them

Some of the methods you would need would be:

Date.prototype.getHours()
Date.prototype.setHours()

Above assumes that you are trying to actually create a new DateTime. If not it is not clear what your expected results are

Reference: MDN Date.prototype Docs




回答2:


I solved this problem using the same ng-model for Datepicker and Timepicker.

<input type="text" ng-model="myDateModel" datepicker-options="dateOptions" ... />

<uib-timepicker ng-model="myDateModel"...></uib-timepicker>

I hope it helps you too! Good luck!



来源:https://stackoverflow.com/questions/28891396/how-to-combine-values-from-datepicker-timepicker-in-one-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!