如何使用AngularJS将日期分成三个选择框?

时间:2013-11-13 19:03:44

标签: angularjs

我的日期是UNIX时间戳,

dateOfBirth = 1384369855;
$scope.dob = dateOfBirth;

在我看来,如何将其传递给模型以包含三个字段,一个用于daymonthyear

1 个答案:

答案 0 :(得分:3)

<强> HTML

<div ng-app ng-controller="MyCtrl">
    days:   <select ng-model="selectedday" ng-options="selectedday for selectedday in days"></select>
    month:  <select ng-model="selectedmonth" ng-options="selectedmonth for selectedmonth in months"></select>
    years:  <select ng-model="selectedyear" ng-options="selectedyear for selectedyear in years"></select>    
</div>

<强> JS

    $scope.days = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31];
    $scope.months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
    $scope.years = [2010,2011,2012,2013,2014];

     $scope.theDate = new Date(1384369855000);

    $scope.selectedyear = $scope.theDate.getFullYear();
    $scope.selectedmonth = $scope.months[$scope.theDate.getMonth()];
    $scope.selectedday = $scope.theDate.getDate();   

演示 Fiddle