簡體   English   中英

angularjs ui引導日期選擇器意外添加了一天

[英]angularjs ui bootstrap datepicker unexpectedly adding a day

美好的一天,默認情況下,我使用momentjs在ui引導日期選擇器輸入字段中以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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM