Angular.js <input type ="date">更改提交值的格式

use*_*335 5 javascript angularjs angularjs-directive angular-ui-bootstrap

<label>From</label>
<input type="date"  ng-model="startDate"/>
<label>To</label>
<input type="date" ng-model="endDate"/>

<button class="btn btn-success" ng-click="getStatistics(startDate, endDate)">Update</button>
Run Code Online (Sandbox Code Playgroud)

**JS***

$scope.getStatistics = function(startDate, endDate) {
    $http.get('/admin/api/stats?start=' + startDate + '&end=' + endDate).success(function(data) {
        $scope.stats = data;
    });
}
Run Code Online (Sandbox Code Playgroud)

上面的代码在url上按startDate和endDate值,但我遇到的问题是提交的值类似于2014-12-01,我希望我的格式为01-05-2014

有什么建议?

Ale*_*era 0

那么,输入日期将为您提供ISO 格式的日期。如果您希望格式化日期,您可能需要这样的函数:

function formatDate(isoDateString)
{
  var newDate = new Date(isoDateString);
  return ('0' + newDate.getDate()).slice(-2)+"-"+('0' + (newDate.getMonth() + 1)).slice(-2)+"-"+newDate.getFullYear();
}
Run Code Online (Sandbox Code Playgroud)

我在月份中添加 +1,因为 getMonth 返回 0 到 11 之间的数字。 slice(-2) 用于前导 0。