Shamoon
Shamoon

Reputation: 43531

How can I split up a date into three select boxes with AngularJS?

I have my date as a UNIX timestamp,

dateOfBirth = 1384369855;
$scope.dob = dateOfBirth;

In my view, how can I pass that to a model to have three fields, one for day, month and year?

Upvotes: 1

Views: 1976

Answers (1)

Maxim Shoustin
Maxim Shoustin

Reputation: 77904

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

Demo Fiddle

Upvotes: 3

Related Questions