如何在angularjs中制作日期和时间选择器?日期和时间应该在同一个界面?

时间:2014-03-10 07:40:41

标签: javascript php jquery html angularjs

我想在角度js文件中使用日期和时间选择器,当我在文本框中单击时,日期和时间选择器将被加速。在那个弹出窗口中,我需要同时选择日期和时间。

3 个答案:

答案 0 :(得分:1)

是的,您可以使用angularjs datetimepicker ...我按照https://github.com/g00fy-/angular-datepicker 并把它放在我的网站上......

答案 1 :(得分:0)

Bootstrap 3 Datepicker v4 here

有一个角度包装器

您可以查看此演示:http://plnkr.co/edit/kNqCuQ,其中包含一些已修复的问题(作为双向模型绑定)。这是指令(datetime.js):

(function() {
'use strict';

angular
.module('plunker')
.directive('datetimepicker', [
  '$timeout',
  function($timeout) {
    return {
      require: '?ngModel',
      restrict: 'EA',
      scope: {
        datetimepickerOptions: '@',
        onDateChangeFunction: '&',
        onDateClickFunction: '&'
      },
      link: function($scope, $element, $attrs, controller) {
        $element.on('dp.change', function() {
          $timeout(function() {
            var dtp = $element.data('DateTimePicker');
            controller.$setViewValue(dtp.date());
            $scope.onDateChangeFunction();
          });
        });

        $element.on('click', function() {
          $scope.onDateClickFunction();
        });

        controller.$render = function() {
          if (!!controller && !!controller.$viewValue) {
            var result = controller.$viewValue;
            $element.data('DateTimePicker').date(result);
          }
        };

        $element.datetimepicker($scope.$eval($attrs.datetimepickerOptions));
      }
    };
  }
]);

})(); 

脚本(script.js):

(function(angular) {
'use strict';

angular.module('plunker', [])
 .controller('controller', ['$scope', '$timeout', function($scope, $timeout) {
    var vm = this;

    vm.date = new Date();
    vm.options = '{format:"DD.MM.YYYY HH:mm"}'


  }]);
})(window.angular);

和html

<!doctype html>
<html lang="en">

 <head>
  <meta charset="UTF-8">
  <title>Example - example-example21-production</title>

  <script type="text/javascript" src="//code.jquery.com/jquery-2.1.1.min.js"> </script>
  <script src="https://code.angularjs.org/1.4.7/angular.min.js"></script>
  <script type="text/javascript" src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment-with-locales.js"></script>
  <script src="//cdn.rawgit.com/Eonasdan/bootstrap-datetimepicker/e8bddc60e73c1ec2475f827be36e1957af72e2ea/src/js/bootstrap-datetimepicker.js"></script>

  <script src="script.js"></script>
  <script src="datetime.js"></script>

  <link rel="stylesheet" type="text/css" media="screen" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" />
  <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
  <link href="//cdn.rawgit.com/Eonasdan/bootstrap-  datetimepicker/e8bddc60e73c1ec2475f827be36e1957af72e2ea/build/css/bootstrap-   datetimepicker.css" rel="stylesheet">

 </head>

 <body ng-app="plunker">

  <p>// BE WARNED THIS IS VERSION 4.15.35 </p>
  <p>// VERSION 4.17.37 (from bower repo) DOES NOT WORK CORRECTLY</p>
  <p>// LOAD MASTER VERSION OR OLDER ONE</p>

  <div ng-controller="controller as vm" style="width: 200px">
    <div class="input-group input-group-sm date" 
    datetimepicker-options="{{vm.options}}" 
    datetimepicker ng-model="vm.date">
    <input type="text" class="form-control" />
      <span class="input-group-addon">
        <span class="glyphicon glyphicon-calendar"></span>
      </span>
    </div>

    {{vm.date}}
   </div>

  </body>

</html>

答案 2 :(得分:0)

var app = angular.module('app',['ui.bootstrap','ui.bootstrap.datetimepicker']);

app.controller('MyController',['$ scope',函数($ scope){

var that = this;

// date picker
this.picker1 = {
    date: new Date('2015-03-01T00:00:00Z'),
    datepickerOptions: {
        showWeeks: false,
        startingDay: 1,
        dateDisabled: function(data) {
            return (data.mode === 'day' && (new Date().toDateString() == data.date.toDateString()));
        }
    }
};

// time picker
this.picker2 = {
    date: new Date('2015-03-01T12:30:00Z'),
    timepickerOptions: {
        readonlyInput: false,
        showMeridian: false
    }
};

// date and time picker
this.picker3 = {
    date: new Date()
};

// min date picker
this.picker4 = {
    date: new Date(),
    datepickerOptions: {
        maxDate: null
    }
};

// max date picker
this.picker5 = {
    date: new Date(),
    datepickerOptions: {
        minDate: null
    }
};

// set date for max picker, 10 days in future
this.picker5.date.setDate(this.picker5.date.getDate() + 10);

// global config picker
this.picker6 = {
    date: new Date()
};

// dropdown up picker
this.picker7 = {
    date: new Date()
};

// view mode picker
this.picker8 = {
    date: new Date(),
    datepickerOptions: {
        mode: 'year',
        minMode: 'year',
        maxMode: 'year'
    }
};

// dropdown up picker
this.picker9 = {
    date: null
};

// min time picker
this.picker10 = {
    date: new Date('2016-03-01T09:00:00Z'),
    timepickerOptions: {
        max: null
    }
};

// max time picker
this.picker11 = {
    date: new Date('2016-03-01T10:00:00Z'),
    timepickerOptions: {
        min: null
    }
};

// button bar
this.picker12 = {
    date: new Date(),
    buttonBar: {
        show: true,
        now: {
            show: true,
            text: 'Now!'
        },
        today: {
            show: true,
            text: 'Today!'
        },
        clear: {
            show: false,
            text: 'Wipe'
        },
        date: {
            show: true,
            text: 'Date'
        },
        time: {
            show: true,
            text: 'Time'
        },
        close: {
            show: true,
            text: 'Shut'
        },
        cancel: {
            show: true,
            text: 'Cancel'
        }
    }
};

// when closed picker
this.picker13 = {
    date: new Date(),
    closed: function(args) {
        that.closedArgs = args;
    }
};

// saveAs - ISO
this.picker14 = {
    date: new Date().toISOString()
}

this.openCalendar = function(e, picker) {
    that[picker].open = true;
};

// watch min and max dates to calculate difference
var unwatchMinMaxValues = $scope.$watch(function() {
    return [that.picker4, that.picker5, that.picker10, that.picker11];
}, function() {
    // min max dates
    that.picker4.datepickerOptions.maxDate = that.picker5.date;
    that.picker5.datepickerOptions.minDate = that.picker4.date;

    if (that.picker4.date && that.picker5.date) {
        var diff = that.picker4.date.getTime() - that.picker5.date.getTime();
        that.dayRange = Math.round(Math.abs(diff/(1000*60*60*24)))
    } else {
        that.dayRange = 'n/a';
    }

    // min max times
    that.picker10.timepickerOptions.max = that.picker11.date;
    that.picker11.timepickerOptions.min = that.picker10.date;
}, true);


// destroy watcher
$scope.$on('$destroy', function() {
    unwatchMinMaxValues();
});

}]);