gpt4 book ai didi

javascript - Angular Strap Datepicker : Inconsistent timestamps returned, UTC +0 vs UTC +12 小时

转载 作者:行者123 更新时间:2023-11-29 14:50:41 24 4
gpt4 key购买 nike

情况:

  • 我们正在使用 Angular-Strap 的 datepicker并希望将 UTC 时间戳返回给服务器。
  • 在选择日期(不选择时间)时,我们发现有些计算机返回的时间戳是 0:00,有些是 12:00。
  • 这与浏览器无关,但因计算机而异。

问题:

  • 一些计算机返回 0:00,一些 12:00。

我尝试了什么:

  • 调试时我发现问题出在 angular-strap/dist/modules/datepicker.js#L379 的第 379 行, controller.$dateValue 返回 'Sat Mar 28 1987 12:00:00 GMT+0100 (W. Europe Standard Time)' 而不是 'Sat Mar 28 1987 00:00:00 GMT+0100 (W. Europe Standard Time) ',其他计算机返回的值。

问题:

  • 如何在所有情况下返回 0:00 时间?

最佳答案

我也遇到了同样的问题。我所做的是写了一个指令来转换日期 utc 日期。检查代码示例。这解决了我的问题。

(function (angular) {
angular.module('myApp').directive('datePicker', ['$parse', 'dateFilter', function ($parse, dateFilter) {
var inputDateFormat = 'dd/MM/yyyy';

var utcDateGeneretor = function (dateString) {
if ('undefined' === typeof dateString || '' === dateString) {
return null;
}

var parts = dateString.split('/');
if (3 !== parts.length) {
return null;
}
var year = parseInt(parts[2], 10);
var month = parseInt(parts[1], 10);
var day = parseInt(parts[0], 10);
if (year.toString().length < 4) {
return null;
}

if (month < 0 || year < 0 || day < 1) {
return null;
}

//Javascript month is zero based. thats why always reduce 1 form selected month.
return new Date(Date.UTC(year, (month - 1), day));
};

return {
restrict: 'A',
require: 'ngModel',
link: function (scope, element, attrs, ctrls) {

var mindatecalvalue = parseInt(attrs["mindatecalvalue"], 10);
var maxdatecalvalue = parseInt(attrs["maxdatecalvalue"], 10);
var twoDigitCutoffSpecified = attrs["twoDigitCutoff"];
var twoDigitYear1900cutoff = -1; //If a 2-digityear is less than this number it is treated as being 2000+, otherwise 1900+.
if (twoDigitCutoffSpecified) {
twoDigitYear1900cutoff = parseInt(attrs["twoDigitCutoff"], 10);
if (isNaN(twoDigitYear1900cutoff)) {
twoDigitYear1900cutoff = -1;
} else if (twoDigitYear1900cutoff <= 0) {
twoDigitYear1900cutoff += (new Date().getFullYear() - 2000); //A negative number is interpreted as a number of years before the current year.
}
}

var updateModel = function (dateText) {

var canonicalisedDateText = ensureFullYear(dateText, twoDigitYear1900cutoff);

// call $apply to bring stuff to angular model
scope.$apply(function () {
ctrls.$setViewValue(canonicalisedDateText);
});
ctrls.$render();
};

var ensureFullYear = function (dateText, twoDigitYear1900cutoff) {
var findYearRegex = /(.+[^0-9])([0-9]+)\s*$/;
var dateParts = findYearRegex.exec(dateText);
if ((!dateParts) || (dateParts.length != 3)) {
return dateText; //can't find a year in this date.
}
var originalYearAsStr = dateParts[2];
if (originalYearAsStr.length > 2) {
return dateText; //Not a 2-digit year.
}
var originalYear = parseInt(originalYearAsStr, 10);
var fullYear = 0;
if (originalYear <= twoDigitYear1900cutoff) {
fullYear = 2000 + originalYear;
} else {
fullYear = 1900 + originalYear;
}
var restOfDate = dateParts[1];
return restOfDate + fullYear;
}


ctrls.$formatters.push(function (modelValue) {
return dateFilter(modelValue, inputDateFormat);
});

ctrls.$parsers.push(function (dateString) {
return utcDateGeneretor(dateString);
});
}
};
}]);

})(angular);

关于javascript - Angular Strap Datepicker : Inconsistent timestamps returned, UTC +0 vs UTC +12 小时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26138722/

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