gpt4 book ai didi

javascript - Angular Bootstrap DatePicker UTC 错误输出

转载 作者:行者123 更新时间:2023-11-28 06:46:33 26 4
gpt4 key购买 nike

您好,我在我的应用程序中使用 Angular DatePicker,它按预期工作。我尝试使用 UTC 时间,并且日期选择器使用以下值启动:

scope.model.value = moment.utc().startOf('day').toDate()

此日期的结果:

Tue Oct 27 2015 01:00:00 GMT+0100 (Mitteleuropäische Zeit)

如果我现在想选择一个日期,例如:2016 年 6 月 1 日,scope.model.value 的结果是:

"2016-05-31T23:00:00.000Z"

为什么我的 DatePicker 将我的日期对象更改为另一种格式?我该如何处理输出格式?为什么选择6月1日时日期是5月31日?

我尝试了多种方法,例如删除 UTC 时间信息。例如:(https://gist.github.com/weberste/354a3f0a9ea58e0ea0de):

(function () {
'use strict';
angular
.module('myApp')
.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;
});

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

对于那些有兴趣的人,这里有一个我如何使用选择器的片段:

<datepicker datepicker-localdate ng-model="model.value" min-date="minDate"></datepicker>

也许有人可以帮忙!

最佳答案

在您的应用程序配置中,您可以全局告诉您希望 dapicker 输出格式如何。例如:

datepickerPopupConfig.datepickerPopup = 'dd/MM/yyyy';

文档在这里:angular boostrap datepicker DOC

假设您的应用名称是“myapp”:

angular.module('myapp', [
'ui.bootstrap'
])
.config(function(
datepickerPopupConfig
) {

datepickerPopupConfig.datepickerPopup = 'dd/MM/yyyy';

}
);

关于javascript - Angular Bootstrap DatePicker UTC 错误输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33372579/

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