在没有 time/timezone 的情况下获得 ui.bootstrap.datepicker 值
Get ui.bootstrap.datepicker value without time/timezone
我需要能够在我的 ng-model 中将选定的日期值存储为没有时间的日期。
这是我的观点:
<script type="text/ng-template" id="form_field_datetime">
<h3 style="color:coral ;">{{field.displayName}}</h3>
<br />
<div>
<div ng-controller="dateCtrl">
<p class="input-group">
<input type="text" class="form-control" datepicker-popup="{{format}}" is-open="opened" ng-required="true" ng-model="field.theValues[0]" />
<span class="input-group-btn">
<button type="button" class="btn btn-default" ng-click="open($event)"><i class="glyphicon glyphicon-calendar"></i></button>
</span>
</p>
</div>
</div>
</script>
选择上面显示的日期时,我的 ng-model 中的存储值为:
2012-03-12T22:00:00.000Z
我需要:
2012-03-13
这是控制器(与example中的差不多):
app.controller('dateCtrl', ['$scope',
function ($scope) {
$scope.open = function($event) {
$event.preventDefault();
$event.stopPropagation();
$scope.opened = true;
};
$scope.dateOptions = {
formatYear: 'yy',
startingDay: 0
};
$scope.formats = ['yyyy-MM-dd', 'dd-MMMM-yyyy', 'yyyy/MM/dd', 'dd.MM.yyyy', 'shortDate'];
$scope.format = $scope.formats[0];
}
]);
如何解决这个问题?
我已经使用我之前在问题评论中提到的指令来处理所选日期并删除时间信息。
<script type="text/ng-template" id="form_field_date">
<h3 style="color:coral ;">{{field.displayName}}</h3>
<br />
<div>
<div ng-controller="dateCtrl">
<p class="input-group">
<input datepicker-localdate type="text" class="form-control" datepicker-popup="{{format}}" is-open="opened" ng-required="true" ng-model="field.theValues[0]" />
<span class="input-group-btn">
<button type="button" class="btn btn-default" ng-click="open($event)"><i class="glyphicon glyphicon-calendar"></i></button>
</span>
</p>
</div>
</div>
</script>
控制器(dateCtrl):
app.controller('dateCtrl', ['$scope',
function ($scope) {
$scope.open = function($event) {
$event.preventDefault();
$event.stopPropagation();
$scope.opened = true;
};
$scope.dateOptions = {
formatYear: 'yy',
startingDay: 0
};
$scope.formats = ['yyyy-MM-dd', 'dd-MMMM-yyyy', 'yyyy/MM/dd', 'dd.MM.yyyy', 'shortDate'];
$scope.format = $scope.formats[0];
}
]);
指令(datepicker-localdate):
app.directive('datepickerLocaldate', ['$parse', function ($parse) {
var directive = {
restrict: 'A',
require: ['ngModel'],
link: link
};
return directive;
function link(scope, element, attr, ctrls) {
var ngModelController = ctrls[0];
// called with a JavaScript Date object when picked from the datepicker
ngModelController.$parsers.push(function (viewValue) {
console.log(viewValue);console.log(viewValue);console.log(viewValue);
// undo the timezone adjustment we did during the formatting
viewValue.setMinutes(viewValue.getMinutes() - viewValue.getTimezoneOffset());
// we just want a local date in ISO format
return viewValue.toISOString().substring(0, 10);
});
}
}]);
现在一切正常!
我需要能够在我的 ng-model 中将选定的日期值存储为没有时间的日期。
这是我的观点:
<script type="text/ng-template" id="form_field_datetime">
<h3 style="color:coral ;">{{field.displayName}}</h3>
<br />
<div>
<div ng-controller="dateCtrl">
<p class="input-group">
<input type="text" class="form-control" datepicker-popup="{{format}}" is-open="opened" ng-required="true" ng-model="field.theValues[0]" />
<span class="input-group-btn">
<button type="button" class="btn btn-default" ng-click="open($event)"><i class="glyphicon glyphicon-calendar"></i></button>
</span>
</p>
</div>
</div>
</script>
选择上面显示的日期时,我的 ng-model 中的存储值为:
2012-03-12T22:00:00.000Z
我需要:
2012-03-13
这是控制器(与example中的差不多):
app.controller('dateCtrl', ['$scope',
function ($scope) {
$scope.open = function($event) {
$event.preventDefault();
$event.stopPropagation();
$scope.opened = true;
};
$scope.dateOptions = {
formatYear: 'yy',
startingDay: 0
};
$scope.formats = ['yyyy-MM-dd', 'dd-MMMM-yyyy', 'yyyy/MM/dd', 'dd.MM.yyyy', 'shortDate'];
$scope.format = $scope.formats[0];
}
]);
如何解决这个问题?
我已经使用我之前在问题评论中提到的指令来处理所选日期并删除时间信息。
<script type="text/ng-template" id="form_field_date">
<h3 style="color:coral ;">{{field.displayName}}</h3>
<br />
<div>
<div ng-controller="dateCtrl">
<p class="input-group">
<input datepicker-localdate type="text" class="form-control" datepicker-popup="{{format}}" is-open="opened" ng-required="true" ng-model="field.theValues[0]" />
<span class="input-group-btn">
<button type="button" class="btn btn-default" ng-click="open($event)"><i class="glyphicon glyphicon-calendar"></i></button>
</span>
</p>
</div>
</div>
</script>
控制器(dateCtrl):
app.controller('dateCtrl', ['$scope',
function ($scope) {
$scope.open = function($event) {
$event.preventDefault();
$event.stopPropagation();
$scope.opened = true;
};
$scope.dateOptions = {
formatYear: 'yy',
startingDay: 0
};
$scope.formats = ['yyyy-MM-dd', 'dd-MMMM-yyyy', 'yyyy/MM/dd', 'dd.MM.yyyy', 'shortDate'];
$scope.format = $scope.formats[0];
}
]);
指令(datepicker-localdate):
app.directive('datepickerLocaldate', ['$parse', function ($parse) {
var directive = {
restrict: 'A',
require: ['ngModel'],
link: link
};
return directive;
function link(scope, element, attr, ctrls) {
var ngModelController = ctrls[0];
// called with a JavaScript Date object when picked from the datepicker
ngModelController.$parsers.push(function (viewValue) {
console.log(viewValue);console.log(viewValue);console.log(viewValue);
// undo the timezone adjustment we did during the formatting
viewValue.setMinutes(viewValue.getMinutes() - viewValue.getTimezoneOffset());
// we just want a local date in ISO format
return viewValue.toISOString().substring(0, 10);
});
}
}]);
现在一切正常!