6

Get a combined date from ui.bootstrap date and time

 2 years ago
source link: https://www.codesd.com/item/get-a-combined-date-from-ui-bootstrap-date-and-time.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Get a combined date from ui.bootstrap date and time

advertisements

I'm trying to combine the Datepicker and Timepicker directives to get the date from the first and the time from the second in a combined Date object. I came across some examples that are not using these directives like this one Combining Date and Time input strings as a Date object. However when I try to apply something similar to my case it's not working. Console returns "TypeError: $scope.dt.split is not a function". Above is the function I try to use which is called by $watch.

function tryCombineDateTime() {
    if ($scope.dt && $scope.mytime) {
        var dateParts = $scope.dt.split('-');
        var timeParts = $scope.mytime.split(':');
        if (dateParts && timeParts) {
            dateParts[1] -= 1;
            $scope.fullDate = new Date(Date.UTC.apply(undefined, dateParts.concat(timeParts))).toISOString();
        }
    }
}

Here is a plunker showing the problem. http://plnkr.co/edit/tnbE3LWQTTzLhLWXLCQB?p=preview

I would prefer a solution based on my Plunker as I don't want to install other components like DateTimePicker.


Date format has been changed, so exception is being thrown, Try this

  function tryCombineDateTime() {
    if ($scope.dt && $scope.mytime) {
      var date = $scope.dt.toString();
      var time = $scope.mytime.toString();
      var dateParts = date.split(' ');
      var timeParts = time.split(' ');
      if (dateParts && timeParts) {
        dateParts[4] = timeParts[4]
        $scope.fullDate = new Date(dateParts.join(' ')).toISOString();
      }
    }
  }


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK