gpt4 book ai didi

javascript - angularjs ui bootstrap datepicker意外添加一天

转载 作者:行者123 更新时间:2023-12-03 07:30:13 25 4
gpt4 key购买 nike

美好的一天,默认情况下,我使用 momentjs 在 ui bootstrap datepicker 输入字段中设置了一个类似于 YYYY-MM-DD 格式的日期。日期以我想要的格式正确显示。当我选择不同的日期时,它会正确显示输入字段,但是当我决定控制台日志时,会向其中添加一天的值和时区(T00:00:00.000Z)。这是我的 html 的片段

<div class="row">
<div class="col-md-6">
<label>Drawdown Ent. Date <span style="color: red;">*</span></label>
<input type="text"
class="form-control"
datepicker-popup="yyyy-MM-dd"
ng-model="bookloan.drawdown_ent_date"
is-open="drawdown_ent_date.open"
ng-click="drawdown_ent_date.open = true"
datepicker-options="entDateOptions"
date-disabled="disabled(date, mode)"
close-text="Close" />
</div>
</div>

这是我的 JavaScript 代码的片段:

$scope.bookloan.drawdown_ent_date = moment().format("YYYY-MM-DD");

什么可能导致这种情况?提前致谢..

最佳答案

我通过使用此指令找到了问题的解决方案:

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) {
// 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);
});

// called with a 'yyyy-mm-dd' string to format
ngModelController.$formatters.push(function (modelValue) {
if (!modelValue) {
return undefined;
}
// date constructor will apply timezone deviations from UTC (i.e. if locale is behind UTC 'dt' will be one day behind)
var dt = new Date(modelValue);
// 'undo' the timezone offset again (so we end up on the original date again)
dt.setMinutes(dt.getMinutes() + dt.getTimezoneOffset());
return dt;
});
}
}]);

并将指令名称放置在输入元素中:

<div class="col-md-6">
<label>Drawdown Ent. Date <span style="color: red;">*</span></label>
<input type="text"
class="form-control"
datepicker-local-date
datepicker-popup="yyyy-MM-dd"
ng-model="bookloan.drawdown_ent_date"
is-open="drawdown_ent_date.open"
ng-click="drawdown_ent_date.open = true"
datepicker-options="entDateOptions"
date-disabled="disabled(date, mode)"
close-text="Close" />
</div>
</div>

关于javascript - angularjs ui bootstrap datepicker意外添加一天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35830690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com